MFC the enhancements to the Linux emulation layer so the MegaRAID SAS

management tools can work.  This is not totally connected to the build
yet.  The mfi(4) change have not been MFC'ed yet.  This will be done
shortly.  I'm trying to do this in chunks so I don't totally break
the build on a missed commit.  This has passed make universe a while
ago:
  Enhance the Linux emulation layer to make MegaRAID SAS managements tool happy.
  Add back in a scheme to emulate old type major/minor numbers via hooks into
  stat, linprocfs to return major/minors that Linux app's expect.  Currently
  only /dev/null is always registered.  Drivers can register via the Linux
  type shim similar to the ioctl shim but by using
  linux_device_register_handler/linux_device_unregister_handler functions.
  The structure is:

    struct linux_device_handler {
        char    *bsd_driver_name;
        char    *linux_driver_name;
        char    *bsd_device_name;
        char    *linux_device_name;
        int     linux_major;
        int     linux_minor;
        int     linux_char_device;
    };

  Linprocfs uses this to display the major number of the driver.  The
  soon to be available linsysfs will use it to fill in the driver name.
  Linux_stat uses it to translate the major/minor into Linux type values.

  Note major numbers are dynamically assigned via passing in a -1 for
  the major number so we don't need to keep track of them.

  This is somewhat needed due to us switching to our devfs.
This commit is contained in:
ambrisko 2006-06-15 15:52:05 +00:00
parent 2663a717b0
commit 6ebd888016
7 changed files with 280 additions and 15 deletions

View File

@ -117,6 +117,7 @@ extern int linux_szsigcode;
extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
SET_DECLARE(linux_device_handler_set, struct linux_device_handler);
static int elf_linux_fixup(register_t **stack_base,
struct image_params *iparams);
@ -1036,6 +1037,7 @@ linux_elf_modevent(module_t mod, int type, void *data)
Elf32_Brandinfo **brandinfo;
int error;
struct linux_ioctl_handler **lihp;
struct linux_device_handler **ldhp;
error = 0;
@ -1048,6 +1050,8 @@ linux_elf_modevent(module_t mod, int type, void *data)
if (error == 0) {
SET_FOREACH(lihp, linux_ioctl_handler_set)
linux_ioctl_register_handler(*lihp);
SET_FOREACH(ldhp, linux_device_handler_set)
linux_device_register_handler(*ldhp);
if (bootverbose)
printf("Linux ELF exec handler installed\n");
} else
@ -1067,6 +1071,8 @@ linux_elf_modevent(module_t mod, int type, void *data)
if (error == 0) {
SET_FOREACH(lihp, linux_ioctl_handler_set)
linux_ioctl_unregister_handler(*lihp);
SET_FOREACH(ldhp, linux_device_handler_set)
linux_device_unregister_handler(*ldhp);
if (bootverbose)
printf("Linux ELF exec handler removed\n");
} else

View File

@ -388,14 +388,18 @@ linprocfs_domtab(PFS_FILL_ARGS)
else if (strcmp(fstype, "procfs") == 0)
continue;
sbuf_printf(sb, "%s %s %s %s", mntfrom, mntto, fstype,
mp->mnt_stat.f_flags & MNT_RDONLY ? "ro" : "rw");
if (strcmp(fstype, "linsysfs") == 0) {
sbuf_printf(sb, "/sys %s sysfs %s", mntto,
mp->mnt_stat.f_flags & MNT_RDONLY ? "ro" : "rw");
} else {
sbuf_printf(sb, "%s %s %s %s", mntfrom, mntto, fstype,
mp->mnt_stat.f_flags & MNT_RDONLY ? "ro" : "rw");
}
#define ADD_OPTION(opt, name) \
if (mp->mnt_stat.f_flags & (opt)) sbuf_printf(sb, "," name);
ADD_OPTION(MNT_SYNCHRONOUS, "sync");
ADD_OPTION(MNT_NOEXEC, "noexec");
ADD_OPTION(MNT_NOSUID, "nosuid");
ADD_OPTION(MNT_NODEV, "nodev");
ADD_OPTION(MNT_UNION, "union");
ADD_OPTION(MNT_ASYNC, "async");
ADD_OPTION(MNT_SUIDDIR, "suiddir");
@ -953,7 +957,24 @@ linprocfs_donetdev(PFS_FILL_ARGS)
return (0);
}
#if 0
/*
* Filler function for proc/scsi/device_info
*/
static int
linprocfs_doscsidevinfo(PFS_FILL_ARGS)
{
return (0);
}
/*
* Filler function for proc/scsi/scsi
*/
static int
linprocfs_doscsiscsi(PFS_FILL_ARGS)
{
return (0);
}
extern struct cdevsw *cdevsw[];
/*
@ -962,19 +983,17 @@ extern struct cdevsw *cdevsw[];
static int
linprocfs_dodevices(PFS_FILL_ARGS)
{
int i;
char *char_devices;
sbuf_printf(sb, "Character devices:\n");
for (i = 0; i < NUMCDEVSW; i++)
if (cdevsw[i] != NULL)
sbuf_printf(sb, "%3d %s\n", i, cdevsw[i]->d_name);
char_devices = linux_get_char_devices();
sbuf_printf(sb, "%s", char_devices);
linux_free_get_char_devices(char_devices);
sbuf_printf(sb, "\nBlock devices:\n");
return (0);
}
#endif
/*
* Filler function for proc/cmdline
@ -1020,10 +1039,8 @@ linprocfs_init(PFS_INIT_ARGS)
NULL, NULL, PFS_RD);
pfs_create_file(root, "cpuinfo", &linprocfs_docpuinfo,
NULL, NULL, PFS_RD);
#if 0
pfs_create_file(root, "devices", &linprocfs_dodevices,
NULL, NULL, PFS_RD);
#endif
pfs_create_file(root, "loadavg", &linprocfs_doloadavg,
NULL, NULL, PFS_RD);
pfs_create_file(root, "meminfo", &linprocfs_domeminfo,
@ -1032,6 +1049,8 @@ linprocfs_init(PFS_INIT_ARGS)
pfs_create_file(root, "modules", &linprocfs_domodules,
NULL, NULL, PFS_RD);
#endif
pfs_create_file(root, "mounts", &linprocfs_domtab,
NULL, NULL, PFS_RD);
pfs_create_file(root, "mtab", &linprocfs_domtab,
NULL, NULL, PFS_RD);
pfs_create_link(root, "self", &procfs_docurproc,
@ -1071,6 +1090,12 @@ linprocfs_init(PFS_INIT_ARGS)
pfs_create_file(dir, "status", &linprocfs_doprocstatus,
NULL, NULL, PFS_RD);
/* /proc/scsi/... */
dir = pfs_create_dir(root, "scsi", NULL, NULL, 0);
pfs_create_file(dir, "device_info", &linprocfs_doscsidevinfo,
NULL, NULL, PFS_RD);
pfs_create_file(dir, "scsi", &linprocfs_doscsiscsi,
NULL, NULL, PFS_RD);
return (0);
}

View File

@ -45,8 +45,8 @@ __FBSDID("$FreeBSD$");
#include <sys/syscallsubr.h>
#include <sys/systm.h>
#include <sys/vnode.h>
#include "opt_compat.h"
#include <sys/conf.h>
#include <sys/fcntl.h>
#ifdef COMPAT_LINUX32
#include <machine/../linux32/linux.h>
@ -94,6 +94,51 @@ disk_foo(struct somestat *tbuf)
}
#endif
static void
translate_fd_major_minor(struct thread *td, int fd, struct stat *buf)
{
struct file *fp;
int error;
int major, minor;
if ((error = fget(td, fd, &fp)) != 0)
return;
if (fp->f_vnode) {
if (fp->f_vnode->v_type == VCHR
|| fp->f_vnode->v_type == VBLK) {
if (fp->f_vnode->v_un.vu_cdev) {
if (linux_driver_get_major_minor(
fp->f_vnode->v_un.vu_cdev->si_name,
&major, &minor) == 0) {
buf->st_rdev = (major << 8 | minor);
}
}
}
}
fdrop(fp, td);
}
static void
translate_path_major_minor(struct thread *td, char *path, struct stat *buf)
{
struct proc *p = td->td_proc;
struct filedesc *fdp = p->p_fd;
struct file *fp;
int fd;
int temp;
temp = td->td_retval[0];
if (kern_open(td, path, UIO_SYSSPACE, O_RDONLY, 0) != 0)
return;
fd = td->td_retval[0];
td->td_retval[0] = temp;
translate_fd_major_minor(td, fd, buf);
FILEDESC_LOCK(fdp);
fp = fdp->fd_ofiles[fd];
FILEDESC_UNLOCK(fdp);
fdclose(fdp, fdp->fd_ofiles[fd], fd, td);
}
static int
newstat_copyout(struct stat *buf, void *ubuf)
{
@ -132,6 +177,18 @@ linux_newstat(struct thread *td, struct linux_newstat_args *args)
#endif
error = kern_stat(td, path, UIO_SYSSPACE, &buf);
if (!error && strlen(path) > strlen("/dev/pts/") &&
!strncmp(path, "/dev/pts/", strlen("/dev/pts/"))
&& path[9] >= '0' && path[9] <= '9') {
/*
* Linux checks major and minors of the slave device to make
* sure it's a pty device, so let's make him believe it is.
*/
buf.st_rdev = (136 << 8);
}
translate_path_major_minor(td, path, &buf);
LFREEPATH(path);
if (error)
return (error);
@ -153,6 +210,7 @@ linux_newlstat(struct thread *td, struct linux_newlstat_args *args)
#endif
error = kern_lstat(td, path, UIO_SYSSPACE, &sb);
translate_path_major_minor(td, path, &sb);
LFREEPATH(path);
if (error)
return (error);
@ -171,6 +229,7 @@ linux_newfstat(struct thread *td, struct linux_newfstat_args *args)
#endif
error = kern_fstat(td, args->fd, &buf);
translate_fd_major_minor(td, args->fd, &buf);
if (!error)
error = newstat_copyout(&buf, args->buf);
@ -349,6 +408,18 @@ linux_stat64(struct thread *td, struct linux_stat64_args *args)
#endif
error = kern_stat(td, filename, UIO_SYSSPACE, &buf);
if (!error && strlen(filename) > strlen("/dev/pts/") &&
!strncmp(filename, "/dev/pts/", strlen("/dev/pts/"))
&& filename[9] >= '0' && filename[9] <= '9') {
/*
* Linux checks major and minors of the slave device to make
* sure it's a pty deivce, so let's make him believe it is.
*/
buf.st_rdev = (136 << 8);
}
translate_path_major_minor(td, filename, &buf);
LFREEPATH(filename);
if (error)
return (error);
@ -370,6 +441,7 @@ linux_lstat64(struct thread *td, struct linux_lstat64_args *args)
#endif
error = kern_lstat(td, filename, UIO_SYSSPACE, &sb);
translate_path_major_minor(td, filename, &sb);
LFREEPATH(filename);
if (error)
return (error);
@ -388,6 +460,7 @@ linux_fstat64(struct thread *td, struct linux_fstat64_args *args)
#endif
error = kern_fstat(td, args->fd, &buf);
translate_fd_major_minor(td, args->fd, &buf);
if (!error)
error = stat64_copyout(&buf, args->statbuf);

View File

@ -33,8 +33,10 @@
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/linker_set.h>
#include <sys/mutex.h>
#include <sys/namei.h>
#include <sys/proc.h>
@ -82,3 +84,138 @@ linux_msg(const struct thread *td, const char *fmt, ...)
va_end(ap);
printf("\n");
}
MALLOC_DECLARE(M_LINUX);
struct device_element
{
TAILQ_ENTRY(device_element) list;
struct linux_device_handler entry;
};
static TAILQ_HEAD(, device_element) devices =
TAILQ_HEAD_INITIALIZER(devices);
static struct linux_device_handler null_handler =
{ "mem", "mem", "null", "null", 1, 3, 1};
DATA_SET(linux_device_handler_set, null_handler);
char *
linux_driver_get_name_dev(device_t dev)
{
struct device_element *de;
const char *device_name = device_get_name(dev);
if (device_name == NULL)
return NULL;
TAILQ_FOREACH(de, &devices, list) {
if (strcmp(device_name, de->entry.bsd_driver_name) == 0)
return (de->entry.linux_driver_name);
}
return NULL;
}
int
linux_driver_get_major_minor(char *node, int *major, int *minor)
{
struct device_element *de;
if (node == NULL || major == NULL || minor == NULL)
return 1;
TAILQ_FOREACH(de, &devices, list) {
if (strcmp(node, de->entry.bsd_device_name) == 0) {
*major = de->entry.linux_major;
*minor = de->entry.linux_minor;
return 0;
}
}
return 1;
}
char *
linux_get_char_devices()
{
struct device_element *de;
char *temp, *string, *last;
char formated[256];
int current_size = 0, string_size = 1024;
MALLOC(string, char *, string_size, M_LINUX, M_WAITOK);
string[0] = '\000';
last = "";
TAILQ_FOREACH(de, &devices, list) {
if (!de->entry.linux_char_device)
continue;
temp = string;
if (strcmp(last, de->entry.bsd_driver_name) != 0) {
last = de->entry.bsd_driver_name;
snprintf(formated, sizeof(formated), "%3d %s\n",
de->entry.linux_major,
de->entry.linux_device_name);
if (strlen(formated) + current_size
>= string_size) {
string_size *= 2;
MALLOC(string, char *, string_size,
M_LINUX, M_WAITOK);
bcopy(temp, string, current_size);
FREE(temp, M_LINUX);
}
strcat(string, formated);
current_size = strlen(string);
}
}
return string;
}
void
linux_free_get_char_devices(char *string)
{
FREE(string, M_LINUX);
}
static int linux_major_starting = 200;
int
linux_device_register_handler(struct linux_device_handler *d)
{
struct device_element *de;
if (d == NULL)
return (EINVAL);
MALLOC(de, struct device_element *, sizeof(*de),
M_LINUX, M_WAITOK);
if (d->linux_major < 0) {
d->linux_major = linux_major_starting++;
}
bcopy(d, &de->entry, sizeof(*d));
/* Add the element to the list, sorted on span. */
TAILQ_INSERT_TAIL(&devices, de, list);
return (0);
}
int
linux_device_unregister_handler(struct linux_device_handler *d)
{
struct device_element *de;
if (d == NULL)
return (EINVAL);
TAILQ_FOREACH(de, &devices, list) {
if (bcmp(d, &de->entry, sizeof(*d)) == 0) {
TAILQ_REMOVE(&devices, de, list);
FREE(de, M_LINUX);
return (0);
}
}
return (EINVAL);
}

View File

@ -48,6 +48,7 @@
#include <sys/syslog.h>
#include <sys/cdefs.h>
#include <sys/uio.h>
#include <sys/bus.h>
static __inline caddr_t stackgap_init(void);
static __inline void *stackgap_alloc(caddr_t *, size_t);
@ -110,4 +111,21 @@ struct __hack
void linux_msg(const struct thread *td, const char *fmt, ...)
__printflike(2, 3);
struct linux_device_handler {
char *bsd_driver_name;
char *linux_driver_name;
char *bsd_device_name;
char *linux_device_name;
int linux_major;
int linux_minor;
int linux_char_device;
};
int linux_device_register_handler(struct linux_device_handler *h);
int linux_device_unregister_handler(struct linux_device_handler *h);
char *linux_driver_get_name_dev(device_t dev);
int linux_driver_get_major_minor(char *node, int *major, int *minor);
char *linux_get_char_devices(void);
void linux_free_get_char_devices(char *string);
#endif /* !_LINUX_UTIL_H_ */

View File

@ -103,6 +103,7 @@ extern int linux_szsigcode;
extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
SET_DECLARE(linux_device_handler_set, struct linux_device_handler);
static int linux_fixup(register_t **stack_base,
struct image_params *iparams);
@ -879,6 +880,7 @@ linux_elf_modevent(module_t mod, int type, void *data)
Elf32_Brandinfo **brandinfo;
int error;
struct linux_ioctl_handler **lihp;
struct linux_device_handler **ldhp;
error = 0;
@ -891,6 +893,8 @@ linux_elf_modevent(module_t mod, int type, void *data)
if (error == 0) {
SET_FOREACH(lihp, linux_ioctl_handler_set)
linux_ioctl_register_handler(*lihp);
SET_FOREACH(ldhp, linux_device_handler_set)
linux_device_register_handler(*ldhp);
if (bootverbose)
printf("Linux ELF exec handler installed\n");
} else
@ -910,6 +914,8 @@ linux_elf_modevent(module_t mod, int type, void *data)
if (error == 0) {
SET_FOREACH(lihp, linux_ioctl_handler_set)
linux_ioctl_unregister_handler(*lihp);
SET_FOREACH(ldhp, linux_device_handler_set)
linux_device_unregister_handler(*ldhp);
if (bootverbose)
printf("Linux ELF exec handler removed\n");
} else

View File

@ -7,7 +7,7 @@ SRCS= linux_dummy.c linux_file.c linux_getcwd.c linux_ioctl.c linux_ipc.c \
linux_machdep.c linux_mib.c linux_misc.c linux_signal.c linux_socket.c \
linux_stats.c linux_sysctl.c linux_sysent.c linux_sysvec.c \
linux_util.c opt_compat.h opt_inet6.h opt_mac.h \
opt_vmpage.h vnode_if.h
opt_vmpage.h vnode_if.h device_if.h bus_if.h
OBJS= linux_locore.o
.if ${MACHINE_ARCH} == "i386"