Whamcloud - gitweb
LU-5275 lprocfs: remove last of non seq data structs and functions.
[fs/lustre-release.git] / lustre / obdclass / linux / linux-module.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/linux/linux-module.c
37  *
38  * Object Devices Class Driver
39  * These are the only exported functions, they provide some generic
40  * infrastructure for managing object devices
41  */
42
43 #define DEBUG_SUBSYSTEM S_CLASS
44
45 #include <linux/module.h>
46 #include <linux/errno.h>
47 #include <linux/kernel.h>
48 #include <linux/major.h>
49 #include <linux/sched.h>
50 #include <linux/lp.h>
51 #include <linux/slab.h>
52 #include <linux/ioport.h>
53 #include <linux/fcntl.h>
54 #include <linux/delay.h>
55 #include <linux/skbuff.h>
56 #include <linux/proc_fs.h>
57 #include <linux/fs.h>
58 #include <linux/poll.h>
59 #include <linux/init.h>
60 #include <linux/list.h>
61 #include <linux/highmem.h>
62 #include <asm/io.h>
63 #include <asm/ioctls.h>
64 #include <asm/poll.h>
65 #include <asm/uaccess.h>
66 #include <linux/miscdevice.h>
67 #include <linux/seq_file.h>
68
69 #include <libcfs/libcfs.h>
70 #include <obd_support.h>
71 #include <obd_class.h>
72 #include <lnet/lnetctl.h>
73 #include <lprocfs_status.h>
74 #include <lustre_ioctl.h>
75 #include <lustre_ver.h>
76 #include <lustre/lustre_build_version.h>
77
78 int proc_version;
79
80 /* buffer MUST be at least the size of obd_ioctl_hdr */
81 int obd_ioctl_getdata(char **buf, int *len, void __user *arg)
82 {
83         struct obd_ioctl_hdr hdr;
84         struct obd_ioctl_data *data;
85         int offset = 0;
86         ENTRY;
87
88         if (copy_from_user(&hdr, arg, sizeof(hdr)))
89                 RETURN(-EFAULT);
90
91         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
92                 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
93                        OBD_IOCTL_VERSION, hdr.ioc_version);
94                 RETURN(-EINVAL);
95         }
96
97         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
98                 CERROR("User buffer len %d exceeds %d max buffer\n",
99                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
100                 RETURN(-EINVAL);
101         }
102
103         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
104                 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
105                 RETURN(-EINVAL);
106         }
107
108         /* When there are lots of processes calling vmalloc on multi-core
109          * system, the high lock contention will hurt performance badly,
110          * obdfilter-survey is an example, which relies on ioctl. So we'd
111          * better avoid vmalloc on ioctl path. LU-66 */
112         OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
113         if (*buf == NULL) {
114                 CERROR("Cannot allocate control buffer of len %d\n",
115                        hdr.ioc_len);
116                 RETURN(-EINVAL);
117         }
118         *len = hdr.ioc_len;
119         data = (struct obd_ioctl_data *)*buf;
120
121         if (copy_from_user(*buf, arg, hdr.ioc_len)) {
122                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
123                 RETURN(-EFAULT);
124         }
125
126         if (obd_ioctl_is_invalid(data)) {
127                 CERROR("ioctl not correctly formatted\n");
128                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
129                 RETURN(-EINVAL);
130         }
131
132         if (data->ioc_inllen1) {
133                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
134                 offset += cfs_size_round(data->ioc_inllen1);
135         }
136
137         if (data->ioc_inllen2) {
138                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
139                 offset += cfs_size_round(data->ioc_inllen2);
140         }
141
142         if (data->ioc_inllen3) {
143                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
144                 offset += cfs_size_round(data->ioc_inllen3);
145         }
146
147         if (data->ioc_inllen4)
148                 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
149
150         RETURN(0);
151 }
152 EXPORT_SYMBOL(obd_ioctl_getdata);
153
154 int obd_ioctl_popdata(void __user *arg, void *data, int len)
155 {
156         int err;
157         ENTRY;
158
159         err = copy_to_user(arg, data, len) ? -EFAULT : 0;
160         RETURN(err);
161 }
162 EXPORT_SYMBOL(obd_ioctl_popdata);
163
164 /*  opening /dev/obd */
165 static int obd_class_open(struct inode * inode, struct file * file)
166 {
167         ENTRY;
168
169         try_module_get(THIS_MODULE);
170         RETURN(0);
171 }
172
173 /*  closing /dev/obd */
174 static int obd_class_release(struct inode * inode, struct file * file)
175 {
176         ENTRY;
177
178         module_put(THIS_MODULE);
179         RETURN(0);
180 }
181
182 /* to control /dev/obd */
183 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
184                             unsigned long arg)
185 {
186         int err = 0;
187         ENTRY;
188
189         /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
190         if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
191                 RETURN(err = -EACCES);
192         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
193                 RETURN(err = -ENOTTY);
194
195         err = class_handle_ioctl(cmd, (unsigned long)arg);
196
197         RETURN(err);
198 }
199
200 /* declare character device */
201 static struct file_operations obd_psdev_fops = {
202         .owner          = THIS_MODULE,
203         .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
204         .open           = obd_class_open,      /* open */
205         .release        = obd_class_release,   /* release */
206 };
207
208 /* modules setup */
209 struct miscdevice obd_psdev = {
210         .minor = OBD_DEV_MINOR,
211         .name  = OBD_DEV_NAME,
212         .fops  = &obd_psdev_fops,
213 };
214
215
216 #ifdef LPROCFS
217 static int obd_proc_version_seq_show(struct seq_file *m, void *v)
218 {
219         return seq_printf(m, "lustre: %s\nkernel: %s\nbuild:  %s\n",
220                           LUSTRE_VERSION_STRING, "patchless_client",
221                           BUILD_VERSION);
222 }
223 LPROC_SEQ_FOPS_RO(obd_proc_version);
224
225 static int obd_proc_pinger_seq_show(struct seq_file *m, void *v)
226 {
227         return seq_printf(m, "%s\n",
228 #ifdef ENABLE_PINGER
229                              "on"
230 #else
231                              "off"
232 #endif
233                          );
234 }
235 LPROC_SEQ_FOPS_RO(obd_proc_pinger);
236
237 /**
238  * Check all obd devices health
239  *
240  * \param seq_file
241  * \param data [in] unused
242  *
243  * \retval number of characters printed if healthy
244  */
245 static int obd_proc_health_seq_show(struct seq_file *m, void *data)
246 {
247         bool healthy = true;
248         int i;
249
250         if (libcfs_catastrophe)
251                 seq_printf(m, "LBUG\n");
252
253         read_lock(&obd_dev_lock);
254         for (i = 0; i < class_devno_max(); i++) {
255                 struct obd_device *obd;
256
257                 obd = class_num2obd(i);
258                 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
259                         continue;
260
261                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
262                 if (obd->obd_stopping)
263                         continue;
264
265                 class_incref(obd, __FUNCTION__, current);
266                 read_unlock(&obd_dev_lock);
267
268                 if (obd_health_check(NULL, obd)) {
269                         seq_printf(m, "device %s reported unhealthy\n",
270                                         obd->obd_name);
271                         healthy = false;
272                 }
273                 class_decref(obd, __FUNCTION__, current);
274                 read_lock(&obd_dev_lock);
275         }
276         read_unlock(&obd_dev_lock);
277
278         if (healthy)
279                 return seq_printf(m, "healthy\n");
280
281         seq_printf(m, "NOT HEALTHY\n");
282         return 0;
283 }
284 LPROC_SEQ_FOPS_RO(obd_proc_health);
285
286 static int obd_proc_jobid_var_seq_show(struct seq_file *m, void *v)
287 {
288         return seq_printf(m, "%s\n", obd_jobid_var);
289 }
290
291 static ssize_t
292 obd_proc_jobid_var_seq_write(struct file *file, const char __user *buffer,
293                              size_t count, loff_t *off)
294 {
295         if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
296                 return -EINVAL;
297
298         memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
299
300         /* This might leave the var invalid on error, which is probably fine.*/
301         if (copy_from_user(obd_jobid_var, buffer, count))
302                 return -EFAULT;
303
304         /* Trim the trailing '\n' if any */
305         if (obd_jobid_var[count - 1] == '\n')
306                 obd_jobid_var[count - 1] = 0;
307
308         return count;
309 }
310 LPROC_SEQ_FOPS(obd_proc_jobid_var);
311
312 /* Root for /proc/fs/lustre */
313 struct proc_dir_entry *proc_lustre_root = NULL;
314 EXPORT_SYMBOL(proc_lustre_root);
315
316 struct lprocfs_vars lprocfs_base[] = {
317         { .name =       "version",
318           .fops =       &obd_proc_version_fops  },
319         { .name =       "pinger",
320           .fops =       &obd_proc_pinger_fops   },
321         { .name =       "health_check",
322           .fops =       &obd_proc_health_fops   },
323         { .name =       "jobid_var",
324           .fops =       &obd_proc_jobid_var_fops},
325         { 0 }
326 };
327 #else
328 #define lprocfs_base NULL
329 #endif /* LPROCFS */
330
331 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
332 {
333         if (*pos >= class_devno_max())
334                 return NULL;
335
336         return pos;
337 }
338
339 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
340 {
341 }
342
343 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
344 {
345         ++*pos;
346         if (*pos >= class_devno_max())
347                 return NULL;
348
349         return pos;
350 }
351
352 static int obd_device_list_seq_show(struct seq_file *p, void *v)
353 {
354         loff_t index = *(loff_t *)v;
355         struct obd_device *obd = class_num2obd((int)index);
356         char *status;
357
358         if (obd == NULL)
359                 return 0;
360
361         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
362         if (obd->obd_stopping)
363                 status = "ST";
364         else if (obd->obd_inactive)
365                 status = "IN";
366         else if (obd->obd_set_up)
367                 status = "UP";
368         else if (obd->obd_attached)
369                 status = "AT";
370         else
371                 status = "--";
372
373         return seq_printf(p, "%3d %s %s %s %s %d\n",
374                           (int)index, status, obd->obd_type->typ_name,
375                           obd->obd_name, obd->obd_uuid.uuid,
376                           atomic_read(&obd->obd_refcount));
377 }
378
379 struct seq_operations obd_device_list_sops = {
380         .start = obd_device_list_seq_start,
381         .stop = obd_device_list_seq_stop,
382         .next = obd_device_list_seq_next,
383         .show = obd_device_list_seq_show,
384 };
385
386 static int obd_device_list_open(struct inode *inode, struct file *file)
387 {
388         struct seq_file *seq;
389         int rc = seq_open(file, &obd_device_list_sops);
390
391         if (rc)
392                 return rc;
393
394         seq = file->private_data;
395         seq->private = PDE_DATA(inode);
396         return 0;
397 }
398
399 struct file_operations obd_device_list_fops = {
400         .owner   = THIS_MODULE,
401         .open    = obd_device_list_open,
402         .read    = seq_read,
403         .llseek  = seq_lseek,
404         .release = seq_release,
405 };
406
407 int class_procfs_init(void)
408 {
409         struct proc_dir_entry *entry;
410         int rc;
411         ENTRY;
412
413         obd_sysctl_init();
414
415         entry = lprocfs_register("fs/lustre", NULL, lprocfs_base, NULL);
416         if (IS_ERR(entry)) {
417                 rc = PTR_ERR(entry);
418                 CERROR("cannot create '/proc/fs/lustre': rc = %d\n", rc);
419                 RETURN(rc);
420         }
421
422         proc_lustre_root = entry;
423
424         rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444,
425                                 &obd_device_list_fops, NULL);
426         if (rc < 0) {
427                 CERROR("cannot create '/proc/fs/lustre/devices': rc = %d\n",
428                        rc);
429                 GOTO(out_proc, rc);
430         }
431
432         RETURN(rc);
433
434 out_proc:
435         lprocfs_remove(&proc_lustre_root);
436
437         RETURN(rc);
438 }
439
440 int class_procfs_clean(void)
441 {
442         ENTRY;
443         if (proc_lustre_root) {
444                 lprocfs_remove(&proc_lustre_root);
445         }
446         RETURN(0);
447 }