diff --git a/sys/compat/lindebugfs/lindebugfs.c b/sys/compat/lindebugfs/lindebugfs.c index 2cede0ef213f..d32de5d0657e 100644 --- a/sys/compat/lindebugfs/lindebugfs.c +++ b/sys/compat/lindebugfs/lindebugfs.c @@ -1,668 +1,666 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2016-2018, Matthew Macy * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include MALLOC_DEFINE(M_DFSINT, "debugfsint", "Linux debugfs internal"); static struct pfs_node *debugfs_root; #define DM_SYMLINK 0x1 #define DM_DIR 0x2 #define DM_FILE 0x3 struct dentry_meta { struct dentry dm_dnode; const struct file_operations *dm_fops; void *dm_data; umode_t dm_mode; int dm_type; }; static int debugfs_attr(PFS_ATTR_ARGS) { struct dentry_meta *dm; dm = pn->pn_data; vap->va_mode = dm->dm_mode; return (0); } static int debugfs_destroy(PFS_DESTROY_ARGS) { struct dentry_meta *dm; dm = pn->pn_data; if (dm->dm_type == DM_SYMLINK) free(dm->dm_data, M_DFSINT); free(dm, M_DFSINT); return (0); } static int debugfs_fill(PFS_FILL_ARGS) { struct dentry_meta *d; struct linux_file lf = {}; struct vnode vn; char *buf; int rc; off_t off = 0; if ((rc = linux_set_current_flags(curthread, M_NOWAIT))) return (rc); d = pn->pn_data; vn.v_data = d->dm_data; rc = d->dm_fops->open(&vn, &lf); if (rc < 0) { #ifdef INVARIANTS printf("%s:%d open failed with %d\n", __FUNCTION__, __LINE__, rc); #endif return (-rc); } rc = -ENODEV; switch (uio->uio_rw) { case UIO_READ: if (d->dm_fops->read != NULL) { rc = -ENOMEM; buf = malloc(sb->s_size, M_DFSINT, M_ZERO | M_NOWAIT); if (buf != NULL) { rc = d->dm_fops->read(&lf, buf, sb->s_size, &off); if (rc > 0) sbuf_bcpy(sb, buf, strlen(buf)); free(buf, M_DFSINT); } } break; case UIO_WRITE: if (d->dm_fops->write != NULL) { sbuf_finish(sb); rc = d->dm_fops->write(&lf, sbuf_data(sb), sbuf_len(sb), &off); } break; } if (d->dm_fops->release) d->dm_fops->release(&vn, &lf); - else - single_release(&vn, &lf); if (rc < 0) { #ifdef INVARIANTS printf("%s:%d read/write failed with %d\n", __FUNCTION__, __LINE__, rc); #endif return (-rc); } return (0); } static int debugfs_fill_data(PFS_FILL_ARGS) { struct dentry_meta *dm; dm = pn->pn_data; sbuf_printf(sb, "%s", (char *)dm->dm_data); return (0); } struct dentry * debugfs_create_file(const char *name, umode_t mode, struct dentry *parent, void *data, const struct file_operations *fops) { struct dentry_meta *dm; struct dentry *dnode; struct pfs_node *pnode; int flags; dm = malloc(sizeof(*dm), M_DFSINT, M_NOWAIT | M_ZERO); if (dm == NULL) return (NULL); dnode = &dm->dm_dnode; dm->dm_fops = fops; dm->dm_data = data; dm->dm_mode = mode; dm->dm_type = DM_FILE; if (parent != NULL) pnode = parent->d_pfs_node; else pnode = debugfs_root; flags = fops->write ? PFS_RDWR : PFS_RD; dnode->d_pfs_node = pfs_create_file(pnode, name, debugfs_fill, debugfs_attr, NULL, debugfs_destroy, flags | PFS_NOWAIT); if (dnode->d_pfs_node == NULL) { free(dm, M_DFSINT); return (NULL); } dnode->d_pfs_node->pn_data = dm; return (dnode); } struct dentry * debugfs_create_file_size(const char *name, umode_t mode, struct dentry *parent, void *data, const struct file_operations *fops, loff_t file_size __unused) { return debugfs_create_file(name, mode, parent, data, fops); } /* * NOTE: Files created with the _unsafe moniker will not be protected from * debugfs core file removals. It is the responsibility of @fops to protect * its file using debugfs_file_get() and debugfs_file_put(). * * FreeBSD's LinuxKPI lindebugfs does not perform file removals at the time * of writing. Therefore there is no difference between functions with _unsafe * and functions without _unsafe when using lindebugfs. Functions with _unsafe * exist only for Linux compatibility. */ struct dentry * debugfs_create_file_unsafe(const char *name, umode_t mode, struct dentry *parent, void *data, const struct file_operations *fops) { return (debugfs_create_file(name, mode, parent, data, fops)); } struct dentry * debugfs_create_mode_unsafe(const char *name, umode_t mode, struct dentry *parent, void *data, const struct file_operations *fops, const struct file_operations *fops_ro, const struct file_operations *fops_wo) { umode_t read = mode & S_IRUGO; umode_t write = mode & S_IWUGO; if (read && !write) return (debugfs_create_file_unsafe(name, mode, parent, data, fops_ro)); if (write && !read) return (debugfs_create_file_unsafe(name, mode, parent, data, fops_wo)); return (debugfs_create_file_unsafe(name, mode, parent, data, fops)); } struct dentry * debugfs_create_dir(const char *name, struct dentry *parent) { struct dentry_meta *dm; struct dentry *dnode; struct pfs_node *pnode; dm = malloc(sizeof(*dm), M_DFSINT, M_NOWAIT | M_ZERO); if (dm == NULL) return (NULL); dnode = &dm->dm_dnode; dm->dm_mode = 0700; dm->dm_type = DM_DIR; if (parent != NULL) pnode = parent->d_pfs_node; else pnode = debugfs_root; dnode->d_pfs_node = pfs_create_dir(pnode, name, debugfs_attr, NULL, debugfs_destroy, PFS_RD | PFS_NOWAIT); if (dnode->d_pfs_node == NULL) { free(dm, M_DFSINT); return (NULL); } dnode->d_pfs_node->pn_data = dm; return (dnode); } struct dentry * debugfs_create_symlink(const char *name, struct dentry *parent, const char *dest) { struct dentry_meta *dm; struct dentry *dnode; struct pfs_node *pnode; void *data; data = strdup_flags(dest, M_DFSINT, M_NOWAIT); if (data == NULL) return (NULL); dm = malloc(sizeof(*dm), M_DFSINT, M_NOWAIT | M_ZERO); if (dm == NULL) goto fail1; dnode = &dm->dm_dnode; dm->dm_mode = 0700; dm->dm_type = DM_SYMLINK; dm->dm_data = data; if (parent != NULL) pnode = parent->d_pfs_node; else pnode = debugfs_root; dnode->d_pfs_node = pfs_create_link(pnode, name, &debugfs_fill_data, NULL, NULL, NULL, PFS_NOWAIT); if (dnode->d_pfs_node == NULL) goto fail; dnode->d_pfs_node->pn_data = dm; return (dnode); fail: free(dm, M_DFSINT); fail1: free(data, M_DFSINT); return (NULL); } void debugfs_remove(struct dentry *dnode) { if (dnode == NULL) return; pfs_destroy(dnode->d_pfs_node); } void debugfs_remove_recursive(struct dentry *dnode) { if (dnode == NULL) return; pfs_destroy(dnode->d_pfs_node); } static int debugfs_bool_get(void *data, uint64_t *ullval) { bool *bval = data; if (*bval) *ullval = 1; else *ullval = 0; return (0); } static int debugfs_bool_set(void *data, uint64_t ullval) { bool *bval = data; if (ullval) *bval = 1; else *bval = 0; return (0); } DEFINE_DEBUGFS_ATTRIBUTE(fops_bool, debugfs_bool_get, debugfs_bool_set, "%llu\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_bool_ro, debugfs_bool_get, NULL, "%llu\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_bool_wo, NULL, debugfs_bool_set, "%llu\n"); void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent, bool *value) { debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool, &fops_bool_ro, &fops_bool_wo); } static int debugfs_u8_get(void *data, uint64_t *value) { uint8_t *u8data = data; *value = *u8data; return (0); } static int debugfs_u8_set(void *data, uint64_t value) { uint8_t *u8data = data; *u8data = (uint8_t)value; return (0); } DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%u\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%u\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%u\n"); void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent, uint8_t *value) { debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8, &fops_u8_ro, &fops_u8_wo); } DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%016llx\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%016llx\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%016llx\n"); void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent, uint8_t *value) { debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8, &fops_x8_ro, &fops_x8_wo); } static int debugfs_u16_get(void *data, uint64_t *value) { uint16_t *u16data = data; *value = *u16data; return (0); } static int debugfs_u16_set(void *data, uint64_t value) { uint16_t *u16data = data; *u16data = (uint16_t)value; return (0); } DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%u\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%u\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%u\n"); void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent, uint16_t *value) { debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16, &fops_u16_ro, &fops_u16_wo); } DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%016llx\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%016llx\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%016llx\n"); void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent, uint16_t *value) { debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16, &fops_x16_ro, &fops_x16_wo); } static int debugfs_u32_get(void *data, uint64_t *value) { uint32_t *u32data = data; *value = *u32data; return (0); } static int debugfs_u32_set(void *data, uint64_t value) { uint32_t *u32data = data; *u32data = (uint32_t)value; return (0); } DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%u\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%u\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%u\n"); void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent, uint32_t *value) { debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32, &fops_u32_ro, &fops_u32_wo); } DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%016llx\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%016llx\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%016llx\n"); void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent, uint32_t *value) { debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32, &fops_x32_ro, &fops_x32_wo); } static int debugfs_u64_get(void *data, uint64_t *value) { uint64_t *u64data = data; *value = *u64data; return (0); } static int debugfs_u64_set(void *data, uint64_t value) { uint64_t *u64data = data; *u64data = (uint64_t)value; return (0); } DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%u\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%u\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%u\n"); void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent, uint64_t *value) { debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64, &fops_u64_ro, &fops_u64_wo); } DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n"); void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent, uint64_t *value) { debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64, &fops_x64_ro, &fops_x64_wo); } static int debugfs_ulong_get(void *data, uint64_t *value) { uint64_t *uldata = data; *value = *uldata; return (0); } static int debugfs_ulong_set(void *data, uint64_t value) { uint64_t *uldata = data; *uldata = value; return (0); } DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, "%llu\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n"); void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent, unsigned long *value) { debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong, &fops_ulong_ro, &fops_ulong_wo); } static int debugfs_atomic_t_get(void *data, uint64_t *value) { atomic_t *atomic_data = data; *value = atomic_read(atomic_data); return (0); } static int debugfs_atomic_t_set(void *data, uint64_t value) { atomic_t *atomic_data = data; atomic_set(atomic_data, (int)value); return (0); } DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get, debugfs_atomic_t_set, "%d\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%d\n"); DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%d\n"); void debugfs_create_atomic_t(const char *name, umode_t mode, struct dentry *parent, atomic_t *value) { debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t, &fops_atomic_t_ro, &fops_atomic_t_wo); } static ssize_t fops_blob_read(struct file *filp, char __user *ubuf, size_t read_size, loff_t *ppos) { struct debugfs_blob_wrapper *blob; blob = filp->private_data; if (blob == NULL) return (-EINVAL); if (blob->size == 0 || blob->data == NULL) return (-EINVAL); return (simple_read_from_buffer(ubuf, read_size, ppos, blob->data, blob->size)); } static int fops_blob_open(struct inode *inode, struct file *filp) { return (simple_open(inode, filp)); } static const struct file_operations __fops_blob_ro = { .owner = THIS_MODULE, .open = fops_blob_open, .read = fops_blob_read, .llseek = no_llseek }; struct dentry * debugfs_create_blob(const char *name, umode_t mode, struct dentry *parent, struct debugfs_blob_wrapper *value) { /* Blobs are read-only. */ return (debugfs_create_file(name, mode & 0444, parent, value, &__fops_blob_ro)); } static int lindebugfs_init(PFS_INIT_ARGS) { debugfs_root = pi->pi_root; (void)debugfs_create_symlink("kcov", NULL, "/dev/kcov"); return (0); } static int lindebugfs_uninit(PFS_INIT_ARGS) { return (0); } PSEUDOFS(lindebugfs, 1, VFCF_JAIL); MODULE_DEPEND(lindebugfs, linuxkpi, 1, 1, 1); diff --git a/sys/compat/linuxkpi/common/include/linux/fs.h b/sys/compat/linuxkpi/common/include/linux/fs.h index 9c763168b0f4..d277b717423f 100644 --- a/sys/compat/linuxkpi/common/include/linux/fs.h +++ b/sys/compat/linuxkpi/common/include/linux/fs.h @@ -1,410 +1,414 @@ /*- * Copyright (c) 2010 Isilon Systems, Inc. * Copyright (c) 2010 iX Systems, Inc. * Copyright (c) 2010 Panasas, Inc. * Copyright (c) 2013-2018 Mellanox Technologies, Ltd. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef _LINUXKPI_LINUX_FS_H_ #define _LINUXKPI_LINUX_FS_H_ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include struct module; struct kiocb; struct iovec; struct dentry; struct page; struct file_lock; struct pipe_inode_info; struct vm_area_struct; struct poll_table_struct; struct files_struct; struct pfs_node; struct linux_cdev; #define inode vnode #define i_cdev v_rdev #define i_private v_data #define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH) #define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH) typedef struct files_struct *fl_owner_t; struct file_operations; struct linux_file_wait_queue { struct wait_queue wq; struct wait_queue_head *wqh; atomic_t state; #define LINUX_FWQ_STATE_INIT 0 #define LINUX_FWQ_STATE_NOT_READY 1 #define LINUX_FWQ_STATE_QUEUED 2 #define LINUX_FWQ_STATE_READY 3 #define LINUX_FWQ_STATE_MAX 4 }; struct linux_file { struct file *_file; const struct file_operations *f_op; void *private_data; int f_flags; int f_mode; /* Just starting mode. */ struct dentry *f_dentry; struct dentry f_dentry_store; struct selinfo f_selinfo; struct sigio *f_sigio; struct vnode *f_vnode; #define f_inode f_vnode volatile u_int f_count; /* anonymous shmem object */ vm_object_t f_shmem; /* kqfilter support */ int f_kqflags; #define LINUX_KQ_FLAG_HAS_READ (1 << 0) #define LINUX_KQ_FLAG_HAS_WRITE (1 << 1) #define LINUX_KQ_FLAG_NEED_READ (1 << 2) #define LINUX_KQ_FLAG_NEED_WRITE (1 << 3) /* protects f_selinfo.si_note */ spinlock_t f_kqlock; struct linux_file_wait_queue f_wait_queue; /* pointer to associated character device, if any */ struct linux_cdev *f_cdev; struct rcu_head rcu; }; #define file linux_file #define fasync_struct sigio * #define fasync_helper(fd, filp, on, queue) \ ({ \ if ((on)) \ *(queue) = &(filp)->f_sigio; \ else \ *(queue) = NULL; \ 0; \ }) #define kill_fasync(queue, sig, pollstat) \ do { \ if (*(queue) != NULL) \ pgsigio(*(queue), (sig), 0); \ } while (0) typedef int (*filldir_t)(void *, const char *, int, off_t, u64, unsigned); struct file_operations { struct module *owner; ssize_t (*read)(struct linux_file *, char __user *, size_t, off_t *); ssize_t (*write)(struct linux_file *, const char __user *, size_t, off_t *); unsigned int (*poll) (struct linux_file *, struct poll_table_struct *); long (*unlocked_ioctl)(struct linux_file *, unsigned int, unsigned long); long (*compat_ioctl)(struct linux_file *, unsigned int, unsigned long); int (*mmap)(struct linux_file *, struct vm_area_struct *); int (*open)(struct inode *, struct file *); int (*release)(struct inode *, struct linux_file *); int (*fasync)(int, struct linux_file *, int); /* Although not supported in FreeBSD, to align with Linux code * we are adding llseek() only when it is mapped to no_llseek which returns * an illegal seek error */ off_t (*llseek)(struct linux_file *, off_t, int); #if 0 /* We do not support these methods. Don't permit them to compile. */ loff_t (*llseek)(struct file *, loff_t, int); ssize_t (*aio_read)(struct kiocb *, const struct iovec *, unsigned long, loff_t); ssize_t (*aio_write)(struct kiocb *, const struct iovec *, unsigned long, loff_t); int (*readdir)(struct file *, void *, filldir_t); int (*ioctl)(struct inode *, struct file *, unsigned int, unsigned long); int (*flush)(struct file *, fl_owner_t id); int (*fsync)(struct file *, struct dentry *, int datasync); int (*aio_fsync)(struct kiocb *, int datasync); int (*lock)(struct file *, int, struct file_lock *); ssize_t (*sendpage)(struct file *, struct page *, int, size_t, loff_t *, int); unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); int (*check_flags)(int); int (*flock)(struct file *, int, struct file_lock *); ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int); ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int); int (*setlease)(struct file *, long, struct file_lock **); #endif }; #define fops_get(fops) (fops) #define replace_fops(f, fops) ((f)->f_op = (fops)) #define FMODE_READ FREAD #define FMODE_WRITE FWRITE #define FMODE_EXEC FEXEC #define FMODE_UNSIGNED_OFFSET 0x2000 int __register_chrdev(unsigned int major, unsigned int baseminor, unsigned int count, const char *name, const struct file_operations *fops); int __register_chrdev_p(unsigned int major, unsigned int baseminor, unsigned int count, const char *name, const struct file_operations *fops, uid_t uid, gid_t gid, int mode); void __unregister_chrdev(unsigned int major, unsigned int baseminor, unsigned int count, const char *name); static inline void unregister_chrdev(unsigned int major, const char *name) { __unregister_chrdev(major, 0, 256, name); } static inline int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops) { return (__register_chrdev(major, 0, 256, name, fops)); } static inline int register_chrdev_p(unsigned int major, const char *name, const struct file_operations *fops, uid_t uid, gid_t gid, int mode) { return (__register_chrdev_p(major, 0, 256, name, fops, uid, gid, mode)); } static inline int register_chrdev_region(dev_t dev, unsigned range, const char *name) { return 0; } static inline void unregister_chrdev_region(dev_t dev, unsigned range) { return; } static inline int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name) { return 0; } /* No current support for seek op in FreeBSD */ static inline int nonseekable_open(struct inode *inode, struct file *filp) { return 0; } static inline int simple_open(struct inode *inode, struct file *filp) { filp->private_data = inode->i_private; return 0; } extern unsigned int linux_iminor(struct inode *); #define iminor(...) linux_iminor(__VA_ARGS__) static inline struct linux_file * get_file(struct linux_file *f) { refcount_acquire(f->_file == NULL ? &f->f_count : &f->_file->f_count); return (f); } static inline bool get_file_rcu(struct linux_file *f) { return (refcount_acquire_if_not_zero( f->_file == NULL ? &f->f_count : &f->_file->f_count)); } static inline struct inode * igrab(struct inode *inode) { int error; error = vget(inode, 0); if (error) return (NULL); return (inode); } static inline void iput(struct inode *inode) { vrele(inode); } static inline loff_t no_llseek(struct file *file, loff_t offset, int whence) { return (-ESPIPE); } static inline loff_t default_llseek(struct file *file, loff_t offset, int whence) { return (no_llseek(file, offset, whence)); } static inline loff_t generic_file_llseek(struct file *file, loff_t offset, int whence) { return (no_llseek(file, offset, whence)); } static inline loff_t noop_llseek(struct linux_file *file, loff_t offset, int whence) { return (file->_file->f_offset); } static inline struct vnode * file_inode(const struct linux_file *file) { return (file->f_vnode); } static inline int call_mmap(struct linux_file *file, struct vm_area_struct *vma) { return (file->f_op->mmap(file, vma)); } static inline void i_size_write(struct inode *inode, loff_t i_size) { } /* * simple_read_from_buffer: copy data from kernel-space origin * buffer into user-space destination buffer * * @dest: destination buffer * @read_size: number of bytes to be transferred * @ppos: starting transfer position pointer * @orig: origin buffer * @buf_size: size of destination and origin buffers * * Return value: * On success, total bytes copied with *ppos incremented accordingly. * On failure, negative value. */ static inline ssize_t simple_read_from_buffer(void __user *dest, size_t read_size, loff_t *ppos, void *orig, size_t buf_size) { - void *read_pos = ((char *) orig) + *ppos; + void *p, *read_pos = ((char *) orig) + *ppos; size_t buf_remain = buf_size - *ppos; - ssize_t num_read; if (buf_remain < 0 || buf_remain > buf_size) return -EINVAL; if (read_size > buf_remain) read_size = buf_remain; - /* copy_to_user returns number of bytes NOT read */ - num_read = read_size - copy_to_user(dest, read_pos, read_size); - if (num_read == 0) - return -EFAULT; - *ppos += num_read; - - return (num_read); + /* + * XXX At time of commit only debugfs consumers could be + * identified. If others will use this function we may + * have to revise this: normally we would call copy_to_user() + * here but lindebugfs will return the result and the + * copyout is done elsewhere for us. + */ + p = memcpy(dest, read_pos, read_size); + if (p != NULL) + *ppos += read_size; + + return (read_size); } MALLOC_DECLARE(M_LSATTR); #define __DEFINE_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt, __wrfunc)\ static inline int \ __fops ## _open(struct inode *inode, struct file *filp) \ { \ return (simple_attr_open(inode, filp, __get, __set, __fmt)); \ } \ static const struct file_operations __fops = { \ .owner = THIS_MODULE, \ .open = __fops ## _open, \ .release = simple_attr_release, \ .read = simple_attr_read, \ .write = __wrfunc, \ .llseek = no_llseek \ } #define DEFINE_SIMPLE_ATTRIBUTE(fops, get, set, fmt) \ __DEFINE_SIMPLE_ATTRIBUTE(fops, get, set, fmt, simple_attr_write) #define DEFINE_SIMPLE_ATTRIBUTE_SIGNED(fops, get, set, fmt) \ __DEFINE_SIMPLE_ATTRIBUTE(fops, get, set, fmt, simple_attr_write_signed) int simple_attr_open(struct inode *inode, struct file *filp, int (*get)(void *, uint64_t *), int (*set)(void *, uint64_t), const char *fmt); int simple_attr_release(struct inode *inode, struct file *filp); ssize_t simple_attr_read(struct file *filp, char *buf, size_t read_size, loff_t *ppos); ssize_t simple_attr_write(struct file *filp, const char *buf, size_t write_size, loff_t *ppos); ssize_t simple_attr_write_signed(struct file *filp, const char *buf, size_t write_size, loff_t *ppos); #endif /* _LINUXKPI_LINUX_FS_H_ */