Whamcloud - gitweb
LU-4563 Fix unsafe userspace access in many proc files
[fs/lustre-release.git] / lustre / obdclass / lprocfs_jobstats.c
index aa8ba78..87a1303 100644 (file)
@@ -19,7 +19,7 @@
  * GPL HEADER END
  */
 /*
- * Copyright (c) 2011, 2012, Intel Corporation.
+ * Copyright (c) 2012, 2013, Intel Corporation.
  * Use is subject to license terms.
  *
  * Author: Niu Yawei <niu@whamcloud.com>
@@ -28,9 +28,6 @@
  * lustre/obdclass/lprocfs_jobstats.c
  */
 
-#ifndef EXPORT_SYMTAB
-# define EXPORT_SYMTAB
-#endif
 #define DEBUG_SUBSYSTEM S_CLASS
 
 #ifndef __KERNEL__
@@ -72,7 +69,7 @@
 struct job_stat {
        cfs_hlist_node_t      js_hash;
        cfs_list_t            js_list;
-       cfs_atomic_t          js_refcount;
+       atomic_t          js_refcount;
        char                  js_jobid[JOBSTATS_JOBID_SIZE];
        time_t                js_timestamp; /* seconds */
        struct lprocfs_stats *js_stats;
@@ -108,7 +105,7 @@ static void job_stat_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
 {
        struct job_stat *job;
        job = cfs_hlist_entry(hnode, struct job_stat, js_hash);
-       cfs_atomic_inc(&job->js_refcount);
+       atomic_inc(&job->js_refcount);
 }
 
 static void job_free(struct job_stat *job)
@@ -207,7 +204,7 @@ static struct job_stat *job_alloc(char *jobid, struct obd_job_stats *jobs)
        job->js_jobstats = jobs;
        CFS_INIT_HLIST_NODE(&job->js_hash);
        CFS_INIT_LIST_HEAD(&job->js_list);
-       cfs_atomic_set(&job->js_refcount, 1);
+       atomic_set(&job->js_refcount, 1);
 
        return job;
 }
@@ -366,10 +363,11 @@ static int inline width(const char *str, int len)
 
 static int lprocfs_jobstats_seq_show(struct seq_file *p, void *v)
 {
-       struct job_stat *job = v;
-       struct lprocfs_stats *s;
-       struct lprocfs_counter ret, *cntr;
-       int i;
+       struct job_stat                 *job = v;
+       struct lprocfs_stats            *s;
+       struct lprocfs_counter          ret;
+       struct lprocfs_counter_header   *cntr_header;
+       int                             i;
 
        if (v == SEQ_START_TOKEN) {
                seq_printf(p, "job_stats:\n");
@@ -381,23 +379,24 @@ static int lprocfs_jobstats_seq_show(struct seq_file *p, void *v)
 
        s = job->js_stats;
        for (i = 0; i < s->ls_num; i++) {
-               cntr = &(s->ls_percpu[0]->lp_cntr[i]);
+               cntr_header = &s->ls_cnt_header[i];
                lprocfs_stats_collect(s, i, &ret);
 
                seq_printf(p, "  %s:%.*s { samples: %11"LPF64"u",
-                          cntr->lc_name, width(cntr->lc_name, 15), spaces,
+                          cntr_header->lc_name,
+                          width(cntr_header->lc_name, 15), spaces,
                           ret.lc_count);
-               if (cntr->lc_units[0] != '\0')
-                       seq_printf(p, ", unit: %5s", cntr->lc_units);
+               if (cntr_header->lc_units[0] != '\0')
+                       seq_printf(p, ", unit: %5s", cntr_header->lc_units);
 
-               if (cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) {
+               if (cntr_header->lc_config & LPROCFS_CNTR_AVGMINMAX) {
                        seq_printf(p, ", min:%8"LPF64"u, max:%8"LPF64"u,"
                                   " sum:%16"LPF64"u",
                                   ret.lc_count ? ret.lc_min : 0,
                                   ret.lc_count ? ret.lc_max : 0,
                                   ret.lc_count ? ret.lc_sum : 0);
                }
-               if (cntr->lc_config & LPROCFS_CNTR_STDDEV) {
+               if (cntr_header->lc_config & LPROCFS_CNTR_STDDEV) {
                        seq_printf(p, ", sumsq: %18"LPF64"u",
                                   ret.lc_count ? ret.lc_sumsquare : 0);
                }
@@ -417,24 +416,22 @@ struct seq_operations lprocfs_jobstats_seq_sops = {
 
 static int lprocfs_jobstats_seq_open(struct inode *inode, struct file *file)
 {
-       struct proc_dir_entry *dp = PDE(inode);
        struct seq_file *seq;
        int rc;
 
-       if (LPROCFS_ENTRY_AND_CHECK(dp))
+       if (LPROCFS_ENTRY_CHECK(PDE(inode)))
                return -ENOENT;
 
        rc = seq_open(file, &lprocfs_jobstats_seq_sops);
-       if (rc) {
-               LPROCFS_EXIT();
+       if (rc)
                return rc;
-       }
        seq = file->private_data;
-       seq->private = dp->data;
+       seq->private = PDE_DATA(inode);
        return 0;
 }
 
-static ssize_t lprocfs_jobstats_seq_write(struct file *file, const char *buf,
+static ssize_t lprocfs_jobstats_seq_write(struct file *file,
+                                         const char __user *buf,
                                          size_t len, loff_t *off)
 {
        struct seq_file *seq = file->private_data;
@@ -443,18 +440,19 @@ static ssize_t lprocfs_jobstats_seq_write(struct file *file, const char *buf,
        int all = 0;
        struct job_stat *job;
 
-       if (!memcmp(buf, "clear", strlen("clear"))) {
-               all = 1;
-       } else if (len < JOBSTATS_JOBID_SIZE) {
-               memset(jobid, 0, JOBSTATS_JOBID_SIZE);
-               /* Trim '\n' if any */
-               if (buf[len - 1] == '\n')
-                       memcpy(jobid, buf, len - 1);
-               else
-                       memcpy(jobid, buf, len);
-       } else {
+       if (len == 0 || len >= JOBSTATS_JOBID_SIZE)
                return -EINVAL;
-       }
+
+       if (copy_from_user(jobid, buf, len))
+               return -EFAULT;
+       jobid[len] = 0;
+
+       /* Trim '\n' if any */
+       if (jobid[len - 1] == '\n')
+               jobid[len - 1] = 0;
+
+       if (strcmp(jobid, "clear") == 0)
+               all = 1;
 
        LASSERT(stats->ojs_hash);
        if (all) {
@@ -523,19 +521,18 @@ int lprocfs_job_stats_init(struct obd_device *obd, int cntr_num,
        stats->ojs_last_cleanup = cfs_time_current_sec();
 
        LPROCFS_WRITE_ENTRY();
-       entry = create_proc_entry("job_stats", 0644, obd->obd_proc_entry);
+       entry = proc_create_data("job_stats", 0644, obd->obd_proc_entry,
+                               &lprocfs_jobstats_seq_fops, stats);
        LPROCFS_WRITE_EXIT();
-       if (entry) {
-               entry->proc_fops = &lprocfs_jobstats_seq_fops;
-               entry->data = stats;
-               RETURN(0);
-       } else {
+       if (entry == NULL) {
                lprocfs_job_stats_fini(obd);
                RETURN(-ENOMEM);
        }
+       RETURN(0);
 }
 EXPORT_SYMBOL(lprocfs_job_stats_init);
 
+#ifndef HAVE_ONLY_PROCFS_SEQ
 int lprocfs_rd_job_interval(char *page, char **start, off_t off,
                            int count, int *eof, void *data)
 {
@@ -549,7 +546,7 @@ int lprocfs_rd_job_interval(char *page, char **start, off_t off,
 }
 EXPORT_SYMBOL(lprocfs_rd_job_interval);
 
-int lprocfs_wr_job_interval(struct file *file, const char *buffer,
+int lprocfs_wr_job_interval(struct file *file, const char __user *buffer,
                            unsigned long count, void *data)
 {
        struct obd_device *obd = (struct obd_device *)data;
@@ -570,5 +567,36 @@ int lprocfs_wr_job_interval(struct file *file, const char *buffer,
 
 }
 EXPORT_SYMBOL(lprocfs_wr_job_interval);
+#endif
+int lprocfs_job_interval_seq_show(struct seq_file *m, void *data)
+{
+       struct obd_device *obd = m->private;
+       struct obd_job_stats *stats;
+
+       LASSERT(obd != NULL);
+       stats = &obd->u.obt.obt_jobstats;
+       return seq_printf(m, "%d\n", stats->ojs_cleanup_interval);
+}
+EXPORT_SYMBOL(lprocfs_job_interval_seq_show);
+
+ssize_t
+lprocfs_job_interval_seq_write(struct file *file, const char *buffer,
+                               size_t count, loff_t *off)
+{
+       struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
+       struct obd_job_stats *stats;
+       int val, rc;
+
+       LASSERT(obd != NULL);
+       stats = &obd->u.obt.obt_jobstats;
 
+       rc = lprocfs_write_helper(buffer, count, &val);
+       if (rc)
+               return rc;
+
+       stats->ojs_cleanup_interval = val;
+       lprocfs_job_cleanup(stats, true);
+       return count;
+}
+EXPORT_SYMBOL(lprocfs_job_interval_seq_write);
 #endif /* LPROCFS*/