Whamcloud - gitweb
LU-3331 obdclass: return errors from class_procfs_init()
[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 *arg)
82 {
83         struct obd_ioctl_hdr hdr;
84         struct obd_ioctl_data *data;
85         int err;
86         int offset = 0;
87         ENTRY;
88
89         err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
90         if ( err )
91                 RETURN(err);
92
93         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
94                 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
95                        OBD_IOCTL_VERSION, hdr.ioc_version);
96                 RETURN(-EINVAL);
97         }
98
99         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
100                 CERROR("User buffer len %d exceeds %d max buffer\n",
101                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
102                 RETURN(-EINVAL);
103         }
104
105         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
106                 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
107                 RETURN(-EINVAL);
108         }
109
110         /* When there are lots of processes calling vmalloc on multi-core
111          * system, the high lock contention will hurt performance badly,
112          * obdfilter-survey is an example, which relies on ioctl. So we'd
113          * better avoid vmalloc on ioctl path. LU-66 */
114         OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
115         if (*buf == NULL) {
116                 CERROR("Cannot allocate control buffer of len %d\n",
117                        hdr.ioc_len);
118                 RETURN(-EINVAL);
119         }
120         *len = hdr.ioc_len;
121         data = (struct obd_ioctl_data *)*buf;
122
123         err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
124         if ( err ) {
125                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
126                 RETURN(err);
127         }
128
129         if (obd_ioctl_is_invalid(data)) {
130                 CERROR("ioctl not correctly formatted\n");
131                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
132                 RETURN(-EINVAL);
133         }
134
135         if (data->ioc_inllen1) {
136                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
137                 offset += cfs_size_round(data->ioc_inllen1);
138         }
139
140         if (data->ioc_inllen2) {
141                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
142                 offset += cfs_size_round(data->ioc_inllen2);
143         }
144
145         if (data->ioc_inllen3) {
146                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
147                 offset += cfs_size_round(data->ioc_inllen3);
148         }
149
150         if (data->ioc_inllen4) {
151                 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
152         }
153
154         EXIT;
155         return 0;
156 }
157 EXPORT_SYMBOL(obd_ioctl_getdata);
158
159 int obd_ioctl_popdata(void *arg, void *data, int len)
160 {
161         int err;
162
163         err = copy_to_user(arg, data, len);
164         if (err)
165                 err = -EFAULT;
166         return err;
167 }
168 EXPORT_SYMBOL(obd_ioctl_popdata);
169
170 /*  opening /dev/obd */
171 static int obd_class_open(struct inode * inode, struct file * file)
172 {
173         ENTRY;
174
175         try_module_get(THIS_MODULE);
176         RETURN(0);
177 }
178
179 /*  closing /dev/obd */
180 static int obd_class_release(struct inode * inode, struct file * file)
181 {
182         ENTRY;
183
184         module_put(THIS_MODULE);
185         RETURN(0);
186 }
187
188 /* to control /dev/obd */
189 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
190                             unsigned long arg)
191 {
192         int err = 0;
193         ENTRY;
194
195         /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
196         if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
197                 RETURN(err = -EACCES);
198         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
199                 RETURN(err = -ENOTTY);
200
201         err = class_handle_ioctl(cmd, (unsigned long)arg);
202
203         RETURN(err);
204 }
205
206 /* declare character device */
207 static struct file_operations obd_psdev_fops = {
208         .owner          = THIS_MODULE,
209         .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
210         .open           = obd_class_open,      /* open */
211         .release        = obd_class_release,   /* release */
212 };
213
214 /* modules setup */
215 struct miscdevice obd_psdev = {
216         .minor = OBD_DEV_MINOR,
217         .name  = OBD_DEV_NAME,
218         .fops  = &obd_psdev_fops,
219 };
220
221
222 #ifdef LPROCFS
223 static int obd_proc_version_seq_show(struct seq_file *m, void *v)
224 {
225         return seq_printf(m, "lustre: %s\nkernel: %s\nbuild:  %s\n",
226                           LUSTRE_VERSION_STRING, "patchless_client",
227                           BUILD_VERSION);
228 }
229 LPROC_SEQ_FOPS_RO(obd_proc_version);
230
231 static int obd_proc_pinger_seq_show(struct seq_file *m, void *v)
232 {
233         return seq_printf(m, "%s\n",
234 #ifdef ENABLE_PINGER
235                              "on"
236 #else
237                              "off"
238 #endif
239                          );
240 }
241 LPROC_SEQ_FOPS_RO(obd_proc_pinger);
242
243 /**
244  * Check all obd devices health
245  *
246  * \param seq_file
247  * \param data [in] unused
248  *
249  * \retval number of characters printed if healthy
250  */
251 static int obd_proc_health_seq_show(struct seq_file *m, void *data)
252 {
253         bool healthy = true;
254         int i;
255
256         if (libcfs_catastrophe)
257                 seq_printf(m, "LBUG\n");
258
259         read_lock(&obd_dev_lock);
260         for (i = 0; i < class_devno_max(); i++) {
261                 struct obd_device *obd;
262
263                 obd = class_num2obd(i);
264                 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
265                         continue;
266
267                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
268                 if (obd->obd_stopping)
269                         continue;
270
271                 class_incref(obd, __FUNCTION__, current);
272                 read_unlock(&obd_dev_lock);
273
274                 if (obd_health_check(NULL, obd)) {
275                         seq_printf(m, "device %s reported unhealthy\n",
276                                         obd->obd_name);
277                         healthy = false;
278                 }
279                 class_decref(obd, __FUNCTION__, current);
280                 read_lock(&obd_dev_lock);
281         }
282         read_unlock(&obd_dev_lock);
283
284         if (healthy)
285                 return seq_printf(m, "healthy\n");
286
287         seq_printf(m, "NOT HEALTHY\n");
288         return 0;
289 }
290 LPROC_SEQ_FOPS_RO(obd_proc_health);
291
292 static int obd_proc_jobid_var_seq_show(struct seq_file *m, void *v)
293 {
294         return seq_printf(m, "%s\n", obd_jobid_var);
295 }
296
297 static ssize_t
298 obd_proc_jobid_var_seq_write(struct file *file, const char __user *buffer,
299                              size_t count, loff_t *off)
300 {
301         if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
302                 return -EINVAL;
303
304         memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
305
306         /* This might leave the var invalid on error, which is probably fine.*/
307         if (copy_from_user(obd_jobid_var, buffer, count))
308                 return -EFAULT;
309
310         /* Trim the trailing '\n' if any */
311         if (obd_jobid_var[count - 1] == '\n')
312                 obd_jobid_var[count - 1] = 0;
313
314         return count;
315 }
316 LPROC_SEQ_FOPS(obd_proc_jobid_var);
317
318 /* Root for /proc/fs/lustre */
319 struct proc_dir_entry *proc_lustre_root = NULL;
320 EXPORT_SYMBOL(proc_lustre_root);
321
322 struct lprocfs_seq_vars lprocfs_base[] = {
323         { .name =       "version",
324           .fops =       &obd_proc_version_fops  },
325         { .name =       "pinger",
326           .fops =       &obd_proc_pinger_fops   },
327         { .name =       "health_check",
328           .fops =       &obd_proc_health_fops   },
329         { .name =       "jobid_var",
330           .fops =       &obd_proc_jobid_var_fops},
331         { 0 }
332 };
333 #else
334 #define lprocfs_base NULL
335 #endif /* LPROCFS */
336
337 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
338 {
339         if (*pos >= class_devno_max())
340                 return NULL;
341
342         return pos;
343 }
344
345 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
346 {
347 }
348
349 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
350 {
351         ++*pos;
352         if (*pos >= class_devno_max())
353                 return NULL;
354
355         return pos;
356 }
357
358 static int obd_device_list_seq_show(struct seq_file *p, void *v)
359 {
360         loff_t index = *(loff_t *)v;
361         struct obd_device *obd = class_num2obd((int)index);
362         char *status;
363
364         if (obd == NULL)
365                 return 0;
366
367         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
368         if (obd->obd_stopping)
369                 status = "ST";
370         else if (obd->obd_inactive)
371                 status = "IN";
372         else if (obd->obd_set_up)
373                 status = "UP";
374         else if (obd->obd_attached)
375                 status = "AT";
376         else
377                 status = "--";
378
379         return seq_printf(p, "%3d %s %s %s %s %d\n",
380                           (int)index, status, obd->obd_type->typ_name,
381                           obd->obd_name, obd->obd_uuid.uuid,
382                           atomic_read(&obd->obd_refcount));
383 }
384
385 struct seq_operations obd_device_list_sops = {
386         .start = obd_device_list_seq_start,
387         .stop = obd_device_list_seq_stop,
388         .next = obd_device_list_seq_next,
389         .show = obd_device_list_seq_show,
390 };
391
392 static int obd_device_list_open(struct inode *inode, struct file *file)
393 {
394         struct seq_file *seq;
395         int rc = seq_open(file, &obd_device_list_sops);
396
397         if (rc)
398                 return rc;
399
400         seq = file->private_data;
401         seq->private = PDE_DATA(inode);
402         return 0;
403 }
404
405 struct file_operations obd_device_list_fops = {
406         .owner   = THIS_MODULE,
407         .open    = obd_device_list_open,
408         .read    = seq_read,
409         .llseek  = seq_lseek,
410         .release = seq_release,
411 };
412
413 int class_procfs_init(void)
414 {
415         struct proc_dir_entry *entry;
416         int rc;
417         ENTRY;
418
419         obd_sysctl_init();
420
421         entry = lprocfs_seq_register("fs/lustre", NULL, lprocfs_base, NULL);
422         if (IS_ERR(entry)) {
423                 rc = PTR_ERR(entry);
424                 CERROR("cannot create '/proc/fs/lustre': rc = %d\n", rc);
425                 RETURN(rc);
426         }
427
428         proc_lustre_root = entry;
429
430         rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444,
431                                 &obd_device_list_fops, NULL);
432         if (rc < 0) {
433                 CERROR("cannot create '/proc/fs/lustre/devices': rc = %d\n",
434                        rc);
435                 GOTO(out_proc, rc);
436         }
437
438         RETURN(rc);
439
440 out_proc:
441         lprocfs_remove(&proc_lustre_root);
442
443         RETURN(rc);
444 }
445
446 int class_procfs_clean(void)
447 {
448         ENTRY;
449         if (proc_lustre_root) {
450                 lprocfs_remove(&proc_lustre_root);
451         }
452         RETURN(0);
453 }