Whamcloud - gitweb
LU-694 config: make 'jobid_var' global
[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, Whamcloud, Inc.
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 #ifndef EXPORT_SYMTAB
45 # define EXPORT_SYMTAB
46 #endif
47
48 #ifdef __KERNEL__
49 #ifndef AUTOCONF_INCLUDED
50 #include <linux/config.h> /* for CONFIG_PROC_FS */
51 #endif
52 #include <linux/module.h>
53 #include <linux/errno.h>
54 #include <linux/kernel.h>
55 #include <linux/major.h>
56 #include <linux/sched.h>
57 #include <linux/lp.h>
58 #include <linux/slab.h>
59 #include <linux/ioport.h>
60 #include <linux/fcntl.h>
61 #include <linux/delay.h>
62 #include <linux/skbuff.h>
63 #include <linux/proc_fs.h>
64 #include <linux/fs.h>
65 #include <linux/poll.h>
66 #include <linux/init.h>
67 #include <linux/list.h>
68 #include <linux/highmem.h>
69 #include <asm/io.h>
70 #include <asm/ioctls.h>
71 #include <asm/system.h>
72 #include <asm/poll.h>
73 #include <asm/uaccess.h>
74 #include <linux/miscdevice.h>
75 #include <linux/smp_lock.h>
76 #include <linux/seq_file.h>
77 #else
78 # include <liblustre.h>
79 #endif
80
81 #include <libcfs/libcfs.h>
82 #include <obd_support.h>
83 #include <obd_class.h>
84 #include <lnet/lnetctl.h>
85 #include <lprocfs_status.h>
86 #include <lustre_ver.h>
87 #include <lustre/lustre_build_version.h>
88 #ifdef __KERNEL__
89
90 int proc_version;
91
92 /* buffer MUST be at least the size of obd_ioctl_hdr */
93 int obd_ioctl_getdata(char **buf, int *len, void *arg)
94 {
95         struct obd_ioctl_hdr hdr;
96         struct obd_ioctl_data *data;
97         int err;
98         int offset = 0;
99         ENTRY;
100
101         err = cfs_copy_from_user(&hdr, (void *)arg, sizeof(hdr));
102         if ( err )
103                 RETURN(err);
104
105         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
106                 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
107                        OBD_IOCTL_VERSION, hdr.ioc_version);
108                 RETURN(-EINVAL);
109         }
110
111         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
112                 CERROR("User buffer len %d exceeds %d max buffer\n",
113                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
114                 RETURN(-EINVAL);
115         }
116
117         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
118                 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
119                 RETURN(-EINVAL);
120         }
121
122         /* When there are lots of processes calling vmalloc on multi-core
123          * system, the high lock contention will hurt performance badly,
124          * obdfilter-survey is an example, which relies on ioctl. So we'd
125          * better avoid vmalloc on ioctl path. LU-66 */
126         OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
127         if (*buf == NULL) {
128                 CERROR("Cannot allocate control buffer of len %d\n",
129                        hdr.ioc_len);
130                 RETURN(-EINVAL);
131         }
132         *len = hdr.ioc_len;
133         data = (struct obd_ioctl_data *)*buf;
134
135         err = cfs_copy_from_user(*buf, (void *)arg, hdr.ioc_len);
136         if ( err ) {
137                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
138                 RETURN(err);
139         }
140
141         if (obd_ioctl_is_invalid(data)) {
142                 CERROR("ioctl not correctly formatted\n");
143                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
144                 RETURN(-EINVAL);
145         }
146
147         if (data->ioc_inllen1) {
148                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
149                 offset += cfs_size_round(data->ioc_inllen1);
150         }
151
152         if (data->ioc_inllen2) {
153                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
154                 offset += cfs_size_round(data->ioc_inllen2);
155         }
156
157         if (data->ioc_inllen3) {
158                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
159                 offset += cfs_size_round(data->ioc_inllen3);
160         }
161
162         if (data->ioc_inllen4) {
163                 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
164         }
165
166         EXIT;
167         return 0;
168 }
169
170 int obd_ioctl_popdata(void *arg, void *data, int len)
171 {
172         int err;
173
174         err = cfs_copy_to_user(arg, data, len);
175         if (err)
176                 err = -EFAULT;
177         return err;
178 }
179
180 EXPORT_SYMBOL(obd_ioctl_getdata);
181 EXPORT_SYMBOL(obd_ioctl_popdata);
182
183 /*  opening /dev/obd */
184 static int obd_class_open(struct inode * inode, struct file * file)
185 {
186         ENTRY;
187
188         PORTAL_MODULE_USE;
189         RETURN(0);
190 }
191
192 /*  closing /dev/obd */
193 static int obd_class_release(struct inode * inode, struct file * file)
194 {
195         ENTRY;
196
197         PORTAL_MODULE_UNUSE;
198         RETURN(0);
199 }
200
201 /* to control /dev/obd */
202 #ifdef HAVE_UNLOCKED_IOCTL
203 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
204                             unsigned long arg)
205 #else
206 static int obd_class_ioctl(struct inode *inode, struct file *filp,
207                            unsigned int cmd, unsigned long arg)
208 #endif
209 {
210         int err = 0;
211         ENTRY;
212
213         /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
214         if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
215                 RETURN(err = -EACCES);
216         if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
217                 RETURN(err = -ENOTTY);
218
219         err = class_handle_ioctl(cmd, (unsigned long)arg);
220
221         RETURN(err);
222 }
223
224 /* declare character device */
225 static struct file_operations obd_psdev_fops = {
226         .owner   = THIS_MODULE,
227 #if HAVE_UNLOCKED_IOCTL
228         .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
229 #else
230         .ioctl   = obd_class_ioctl,     /* ioctl */
231 #endif
232         .open    = obd_class_open,      /* open */
233         .release = obd_class_release,   /* release */
234 };
235
236 /* modules setup */
237 cfs_psdev_t obd_psdev = {
238         .minor = OBD_DEV_MINOR,
239         .name  = OBD_DEV_NAME,
240         .fops  = &obd_psdev_fops,
241 };
242
243 #endif
244
245 #ifdef LPROCFS
246 int obd_proc_read_version(char *page, char **start, off_t off, int count,
247                           int *eof, void *data)
248 {
249         *eof = 1;
250         return snprintf(page, count, "lustre: %s\nkernel: %s\nbuild:  %s\n",
251                         LUSTRE_VERSION_STRING, "patchless_client",
252                         BUILD_VERSION);
253 }
254
255 int obd_proc_read_pinger(char *page, char **start, off_t off, int count,
256                          int *eof, void *data)
257 {
258         *eof = 1;
259         return snprintf(page, count, "%s\n",
260 #ifdef ENABLE_PINGER
261                         "on"
262 #else
263                         "off"
264 #endif
265                        );
266 }
267
268 /**
269  * Check all obd devices health
270  *
271  * \param page
272  * \param start
273  * \param off
274  * \param count
275  * \param eof
276  * \param data
277  *                  proc read function parameters, please refer to kernel
278  *                  code fs/proc/generic.c proc_file_read()
279  * \param data [in] unused
280  *
281  * \retval number of characters printed
282  */
283 static int obd_proc_read_health(char *page, char **start, off_t off,
284                                 int count, int *eof, void *data)
285 {
286         int rc = 0, i;
287         *eof = 1;
288
289         if (libcfs_catastrophe)
290                 rc += snprintf(page + rc, count - rc, "LBUG\n");
291
292         cfs_read_lock(&obd_dev_lock);
293         for (i = 0; i < class_devno_max(); i++) {
294                 struct obd_device *obd;
295
296                 obd = class_num2obd(i);
297                 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
298                         continue;
299
300                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
301                 if (obd->obd_stopping)
302                         continue;
303
304                 class_incref(obd, __FUNCTION__, cfs_current());
305                 cfs_read_unlock(&obd_dev_lock);
306
307                 if (obd_health_check(NULL, obd)) {
308                         rc += snprintf(page + rc, count - rc,
309                                        "device %s reported unhealthy\n",
310                                        obd->obd_name);
311                 }
312                 class_decref(obd, __FUNCTION__, cfs_current());
313                 cfs_read_lock(&obd_dev_lock);
314         }
315         cfs_read_unlock(&obd_dev_lock);
316
317         if (rc == 0)
318                 return snprintf(page, count, "healthy\n");
319
320         rc += snprintf(page + rc, count - rc, "NOT HEALTHY\n");
321         return rc;
322 }
323
324 static int obd_proc_rd_jobid_var(char *page, char **start, off_t off,
325                                 int count, int *eof, void *data)
326 {
327         return snprintf(page, count, "%s\n", obd_jobid_var);
328 }
329
330 static int obd_proc_wr_jobid_var(struct file *file, const char *buffer,
331                                 unsigned long count, void *data)
332 {
333         if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
334                 return -EINVAL;
335
336         memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
337         /* Trim the trailing '\n' if any */
338         memcpy(obd_jobid_var, buffer, count - (buffer[count - 1] == '\n'));
339         return count;
340 }
341
342 /* Root for /proc/fs/lustre */
343 struct proc_dir_entry *proc_lustre_root = NULL;
344
345 struct lprocfs_vars lprocfs_base[] = {
346         { "version", obd_proc_read_version, NULL, NULL },
347         { "pinger", obd_proc_read_pinger, NULL, NULL },
348         { "health_check", obd_proc_read_health, NULL, NULL },
349         { "jobid_var", obd_proc_rd_jobid_var,
350                        obd_proc_wr_jobid_var, NULL },
351         { 0 }
352 };
353 #else
354 #define lprocfs_base NULL
355 #endif /* LPROCFS */
356
357 #ifdef __KERNEL__
358 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
359 {
360         if (*pos >= class_devno_max())
361                 return NULL;
362
363         return pos;
364 }
365
366 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
367 {
368 }
369
370 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
371 {
372         ++*pos;
373         if (*pos >= class_devno_max())
374                 return NULL;
375
376         return pos;
377 }
378
379 static int obd_device_list_seq_show(struct seq_file *p, void *v)
380 {
381         loff_t index = *(loff_t *)v;
382         struct obd_device *obd = class_num2obd((int)index);
383         char *status;
384
385         if (obd == NULL)
386                 return 0;
387
388         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
389         if (obd->obd_stopping)
390                 status = "ST";
391         else if (obd->obd_inactive)
392                 status = "IN";
393         else if (obd->obd_set_up)
394                 status = "UP";
395         else if (obd->obd_attached)
396                 status = "AT";
397         else
398                 status = "--";
399
400         return seq_printf(p, "%3d %s %s %s %s %d\n",
401                           (int)index, status, obd->obd_type->typ_name,
402                           obd->obd_name, obd->obd_uuid.uuid,
403                           cfs_atomic_read(&obd->obd_refcount));
404 }
405
406 struct seq_operations obd_device_list_sops = {
407         .start = obd_device_list_seq_start,
408         .stop = obd_device_list_seq_stop,
409         .next = obd_device_list_seq_next,
410         .show = obd_device_list_seq_show,
411 };
412
413 static int obd_device_list_open(struct inode *inode, struct file *file)
414 {
415         struct proc_dir_entry *dp = PDE(inode);
416         struct seq_file *seq;
417         int rc = seq_open(file, &obd_device_list_sops);
418
419         if (rc)
420                 return rc;
421
422         seq = file->private_data;
423         seq->private = dp->data;
424
425         return 0;
426 }
427
428 struct file_operations obd_device_list_fops = {
429         .owner   = THIS_MODULE,
430         .open    = obd_device_list_open,
431         .read    = seq_read,
432         .llseek  = seq_lseek,
433         .release = seq_release,
434 };
435 #endif
436
437 int class_procfs_init(void)
438 {
439 #ifdef __KERNEL__
440         int rc;
441         ENTRY;
442
443         obd_sysctl_init();
444         proc_lustre_root = lprocfs_register("fs/lustre", NULL,
445                                             lprocfs_base, NULL);
446         rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444,
447                                 &obd_device_list_fops, NULL);
448         if (rc)
449                 CERROR("error adding /proc/fs/lustre/devices file\n");
450 #else
451         ENTRY;
452 #endif
453         RETURN(0);
454 }
455
456 #ifdef __KERNEL__
457 int class_procfs_clean(void)
458 {
459         ENTRY;
460         if (proc_lustre_root) {
461                 lprocfs_remove(&proc_lustre_root);
462         }
463         RETURN(0);
464 }
465 #endif