Whamcloud - gitweb
LU-5435 libcfs: copy out ioctl inline buffer
[fs/lustre-release.git] / lustre / obdclass / linux / linux-module.c
index e96802d..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>
 #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 <obd_class.h>
 #include <lnet/lnetctl.h>
 #include <lprocfs_status.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",
@@ -124,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");
@@ -151,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);
 
@@ -222,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)
@@ -300,15 +289,22 @@ static int obd_proc_jobid_var_seq_show(struct seq_file *m, void *v)
 }
 
 static ssize_t
-obd_proc_jobid_var_seq_write(struct file *file, const char *buffer,
-                               size_t count, loff_t *off)
+obd_proc_jobid_var_seq_write(struct file *file, const char __user *buffer,
+                            size_t count, loff_t *off)
 {
        if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
                return -EINVAL;
 
        memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
+
+       /* This might leave the var invalid on error, which is probably fine.*/
+       if (copy_from_user(obd_jobid_var, buffer, count))
+               return -EFAULT;
+
        /* Trim the trailing '\n' if any */
-       memcpy(obd_jobid_var, buffer, count - (buffer[count - 1] == '\n'));
+       if (obd_jobid_var[count - 1] == '\n')
+               obd_jobid_var[count - 1] = 0;
+
        return count;
 }
 LPROC_SEQ_FOPS(obd_proc_jobid_var);
@@ -332,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())
@@ -408,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;
@@ -438,4 +445,3 @@ int class_procfs_clean(void)
         }
         RETURN(0);
 }
-#endif