Whamcloud - gitweb
LU-5435 libcfs: copy out ioctl inline buffer
[fs/lustre-release.git] / lustre / obdclass / linux / linux-module.c
index 0d7f656..8b114d2 100644 (file)
@@ -42,7 +42,6 @@
 
 #define DEBUG_SUBSYSTEM S_CLASS
 
-#ifdef __KERNEL__
 #include <linux/module.h>
 #include <linux/errno.h>
 #include <linux/kernel.h>
@@ -66,9 +65,6 @@
 #include <asm/uaccess.h>
 #include <linux/miscdevice.h>
 #include <linux/seq_file.h>
-#else
-# include <liblustre.h>
-#endif
 
 #include <libcfs/libcfs.h>
 #include <obd_support.h>
 #include <lustre_ioctl.h>
 #include <lustre_ver.h>
 #include <lustre/lustre_build_version.h>
-#ifdef __KERNEL__
 
 int proc_version;
 
 /* buffer MUST be at least the size of obd_ioctl_hdr */
-int obd_ioctl_getdata(char **buf, int *len, void *arg)
+int obd_ioctl_getdata(char **buf, int *len, void __user *arg)
 {
-        struct obd_ioctl_hdr hdr;
-        struct obd_ioctl_data *data;
-        int err;
-        int offset = 0;
-        ENTRY;
+       struct obd_ioctl_hdr hdr;
+       struct obd_ioctl_data *data;
+       int offset = 0;
+       ENTRY;
 
-       err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
-        if ( err )
-                RETURN(err);
+       if (copy_from_user(&hdr, arg, sizeof(hdr)))
+               RETURN(-EFAULT);
 
         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
                 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
@@ -125,11 +118,10 @@ int obd_ioctl_getdata(char **buf, int *len, void *arg)
         *len = hdr.ioc_len;
         data = (struct obd_ioctl_data *)*buf;
 
-       err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
-        if ( err ) {
-                OBD_FREE_LARGE(*buf, hdr.ioc_len);
-                RETURN(err);
-        }
+       if (copy_from_user(*buf, arg, hdr.ioc_len)) {
+               OBD_FREE_LARGE(*buf, hdr.ioc_len);
+               RETURN(-EFAULT);
+       }
 
         if (obd_ioctl_is_invalid(data)) {
                 CERROR("ioctl not correctly formatted\n");
@@ -152,23 +144,20 @@ int obd_ioctl_getdata(char **buf, int *len, void *arg)
                 offset += cfs_size_round(data->ioc_inllen3);
         }
 
-        if (data->ioc_inllen4) {
-                data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
-        }
+       if (data->ioc_inllen4)
+               data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
 
-        EXIT;
-        return 0;
+       RETURN(0);
 }
 EXPORT_SYMBOL(obd_ioctl_getdata);
 
-int obd_ioctl_popdata(void *arg, void *data, int len)
+int obd_ioctl_popdata(void __user *arg, void *data, int len)
 {
        int err;
+       ENTRY;
 
-       err = copy_to_user(arg, data, len);
-       if (err)
-               err = -EFAULT;
-       return err;
+       err = copy_to_user(arg, data, len) ? -EFAULT : 0;
+       RETURN(err);
 }
 EXPORT_SYMBOL(obd_ioctl_popdata);
 
@@ -223,7 +212,6 @@ struct miscdevice obd_psdev = {
         .fops  = &obd_psdev_fops,
 };
 
-#endif
 
 #ifdef LPROCFS
 static int obd_proc_version_seq_show(struct seq_file *m, void *v)
@@ -340,7 +328,6 @@ struct lprocfs_seq_vars lprocfs_base[] = {
 #define lprocfs_base NULL
 #endif /* LPROCFS */
 
-#ifdef __KERNEL__
 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
 {
         if (*pos >= class_devno_max())
@@ -416,28 +403,40 @@ struct file_operations obd_device_list_fops = {
         .llseek  = seq_lseek,
         .release = seq_release,
 };
-#endif
 
 int class_procfs_init(void)
 {
-#ifdef __KERNEL__
+       struct proc_dir_entry *entry;
        int rc;
        ENTRY;
 
        obd_sysctl_init();
-       proc_lustre_root = lprocfs_seq_register("fs/lustre", NULL,
-                                               lprocfs_base, NULL);
+
+       entry = lprocfs_seq_register("fs/lustre", NULL, lprocfs_base, NULL);
+       if (IS_ERR(entry)) {
+               rc = PTR_ERR(entry);
+               CERROR("cannot create '/proc/fs/lustre': rc = %d\n", rc);
+               RETURN(rc);
+       }
+
+       proc_lustre_root = entry;
+
        rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444,
                                &obd_device_list_fops, NULL);
-       if (rc)
-               CERROR("error adding /proc/fs/lustre/devices file\n");
-#else
-       ENTRY;
-#endif
-       RETURN(0);
+       if (rc < 0) {
+               CERROR("cannot create '/proc/fs/lustre/devices': rc = %d\n",
+                      rc);
+               GOTO(out_proc, rc);
+       }
+
+       RETURN(rc);
+
+out_proc:
+       lprocfs_remove(&proc_lustre_root);
+
+       RETURN(rc);
 }
 
-#ifdef __KERNEL__
 int class_procfs_clean(void)
 {
         ENTRY;
@@ -446,4 +445,3 @@ int class_procfs_clean(void)
         }
         RETURN(0);
 }
-#endif