Merge branch 'pm-cpuidle'
[linux-block.git] / drivers / staging / lustre / lustre / obdclass / linux / linux-module.c
CommitLineData
d7e09d03
PT
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 <linux/libcfs/libcfs.h>
70#include <obd_support.h>
71#include <obd_class.h>
72#include <linux/lnet/lnetctl.h>
73#include <lprocfs_status.h>
74#include <lustre_ver.h>
75#include <lustre/lustre_build_version.h>
76
77int proc_version;
78
79/* buffer MUST be at least the size of obd_ioctl_hdr */
80int obd_ioctl_getdata(char **buf, int *len, void *arg)
81{
82 struct obd_ioctl_hdr hdr;
83 struct obd_ioctl_data *data;
84 int err;
85 int offset = 0;
d7e09d03
PT
86
87 err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
88 if ( err )
0a3bdb00 89 return err;
d7e09d03
PT
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);
0a3bdb00 94 return -EINVAL;
d7e09d03
PT
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);
0a3bdb00 100 return -EINVAL;
d7e09d03
PT
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);
0a3bdb00 105 return -EINVAL;
d7e09d03
PT
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);
0a3bdb00 116 return -EINVAL;
d7e09d03
PT
117 }
118 *len = hdr.ioc_len;
119 data = (struct obd_ioctl_data *)*buf;
120
121 err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
122 if ( err ) {
123 OBD_FREE_LARGE(*buf, hdr.ioc_len);
0a3bdb00 124 return err;
d7e09d03
PT
125 }
126
127 if (obd_ioctl_is_invalid(data)) {
128 CERROR("ioctl not correctly formatted\n");
129 OBD_FREE_LARGE(*buf, hdr.ioc_len);
0a3bdb00 130 return -EINVAL;
d7e09d03
PT
131 }
132
133 if (data->ioc_inllen1) {
134 data->ioc_inlbuf1 = &data->ioc_bulk[0];
135 offset += cfs_size_round(data->ioc_inllen1);
136 }
137
138 if (data->ioc_inllen2) {
139 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
140 offset += cfs_size_round(data->ioc_inllen2);
141 }
142
143 if (data->ioc_inllen3) {
144 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
145 offset += cfs_size_round(data->ioc_inllen3);
146 }
147
148 if (data->ioc_inllen4) {
149 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
150 }
151
d7e09d03
PT
152 return 0;
153}
154EXPORT_SYMBOL(obd_ioctl_getdata);
155
156int obd_ioctl_popdata(void *arg, void *data, int len)
157{
158 int err;
159
160 err = copy_to_user(arg, data, len);
161 if (err)
162 err = -EFAULT;
163 return err;
164}
165EXPORT_SYMBOL(obd_ioctl_popdata);
166
167/* opening /dev/obd */
168static int obd_class_open(struct inode * inode, struct file * file)
169{
d7e09d03 170 try_module_get(THIS_MODULE);
0a3bdb00 171 return 0;
d7e09d03
PT
172}
173
174/* closing /dev/obd */
175static int obd_class_release(struct inode * inode, struct file * file)
176{
d7e09d03 177 module_put(THIS_MODULE);
0a3bdb00 178 return 0;
d7e09d03
PT
179}
180
181/* to control /dev/obd */
182static long obd_class_ioctl(struct file *filp, unsigned int cmd,
183 unsigned long arg)
184{
185 int err = 0;
d7e09d03
PT
186
187 /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
188 if (!cfs_capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
0a3bdb00 189 return err = -EACCES;
d7e09d03 190 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
0a3bdb00 191 return err = -ENOTTY;
d7e09d03
PT
192
193 err = class_handle_ioctl(cmd, (unsigned long)arg);
194
0a3bdb00 195 return err;
d7e09d03
PT
196}
197
198/* declare character device */
199static struct file_operations obd_psdev_fops = {
200 .owner = THIS_MODULE,
201 .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
202 .open = obd_class_open, /* open */
203 .release = obd_class_release, /* release */
204};
205
206/* modules setup */
c0426cf7 207struct miscdevice obd_psdev = {
d7e09d03
PT
208 .minor = OBD_DEV_MINOR,
209 .name = OBD_DEV_NAME,
210 .fops = &obd_psdev_fops,
211};
212
213
214#ifdef LPROCFS
73bb1da6 215int obd_proc_version_seq_show(struct seq_file *m, void *v)
d7e09d03 216{
73bb1da6 217 return seq_printf(m, "lustre: %s\nkernel: %s\nbuild: %s\n",
d7e09d03
PT
218 LUSTRE_VERSION_STRING, "patchless_client",
219 BUILD_VERSION);
220}
73bb1da6 221LPROC_SEQ_FOPS_RO(obd_proc_version);
d7e09d03 222
73bb1da6 223int obd_proc_pinger_seq_show(struct seq_file *m, void *v)
d7e09d03 224{
73bb1da6 225 return seq_printf(m, "%s\n", "on");
d7e09d03 226}
73bb1da6 227LPROC_SEQ_FOPS_RO(obd_proc_pinger);
d7e09d03 228
73bb1da6 229static int obd_proc_health_seq_show(struct seq_file *m, void *v)
d7e09d03
PT
230{
231 int rc = 0, i;
d7e09d03
PT
232
233 if (libcfs_catastrophe)
73bb1da6 234 seq_printf(m, "LBUG\n");
d7e09d03
PT
235
236 read_lock(&obd_dev_lock);
237 for (i = 0; i < class_devno_max(); i++) {
238 struct obd_device *obd;
239
240 obd = class_num2obd(i);
241 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
242 continue;
243
244 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
245 if (obd->obd_stopping)
246 continue;
247
248 class_incref(obd, __FUNCTION__, current);
249 read_unlock(&obd_dev_lock);
250
251 if (obd_health_check(NULL, obd)) {
73bb1da6
PT
252 seq_printf(m, "device %s reported unhealthy\n",
253 obd->obd_name);
254 rc++;
d7e09d03
PT
255 }
256 class_decref(obd, __FUNCTION__, current);
257 read_lock(&obd_dev_lock);
258 }
259 read_unlock(&obd_dev_lock);
260
261 if (rc == 0)
73bb1da6 262 return seq_printf(m, "healthy\n");
d7e09d03 263
73bb1da6
PT
264 seq_printf(m, "NOT HEALTHY\n");
265 return 0;
d7e09d03 266}
73bb1da6 267LPROC_SEQ_FOPS_RO(obd_proc_health);
d7e09d03 268
73bb1da6 269static int obd_proc_jobid_var_seq_show(struct seq_file *m, void *v)
d7e09d03 270{
73bb1da6 271 return seq_printf(m, "%s\n", obd_jobid_var);
d7e09d03
PT
272}
273
73bb1da6
PT
274static ssize_t obd_proc_jobid_var_seq_write(struct file *file, const char *buffer,
275 size_t count, loff_t *off)
d7e09d03
PT
276{
277 if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
278 return -EINVAL;
279
280 memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
281 /* Trim the trailing '\n' if any */
282 memcpy(obd_jobid_var, buffer, count - (buffer[count - 1] == '\n'));
283 return count;
284}
73bb1da6 285LPROC_SEQ_FOPS(obd_proc_jobid_var);
d7e09d03
PT
286
287/* Root for /proc/fs/lustre */
288struct proc_dir_entry *proc_lustre_root = NULL;
289EXPORT_SYMBOL(proc_lustre_root);
290
291struct lprocfs_vars lprocfs_base[] = {
73bb1da6
PT
292 { "version", &obd_proc_version_fops },
293 { "pinger", &obd_proc_pinger_fops },
294 { "health_check", &obd_proc_health_fops },
295 { "jobid_var", &obd_proc_jobid_var_fops },
d7e09d03
PT
296 { 0 }
297};
298#else
299#define lprocfs_base NULL
300#endif /* LPROCFS */
301
302static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
303{
304 if (*pos >= class_devno_max())
305 return NULL;
306
307 return pos;
308}
309
310static void obd_device_list_seq_stop(struct seq_file *p, void *v)
311{
312}
313
314static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
315{
316 ++*pos;
317 if (*pos >= class_devno_max())
318 return NULL;
319
320 return pos;
321}
322
323static int obd_device_list_seq_show(struct seq_file *p, void *v)
324{
325 loff_t index = *(loff_t *)v;
326 struct obd_device *obd = class_num2obd((int)index);
327 char *status;
328
329 if (obd == NULL)
330 return 0;
331
332 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
333 if (obd->obd_stopping)
334 status = "ST";
335 else if (obd->obd_inactive)
336 status = "IN";
337 else if (obd->obd_set_up)
338 status = "UP";
339 else if (obd->obd_attached)
340 status = "AT";
341 else
342 status = "--";
343
344 return seq_printf(p, "%3d %s %s %s %s %d\n",
345 (int)index, status, obd->obd_type->typ_name,
346 obd->obd_name, obd->obd_uuid.uuid,
347 atomic_read(&obd->obd_refcount));
348}
349
350struct seq_operations obd_device_list_sops = {
351 .start = obd_device_list_seq_start,
352 .stop = obd_device_list_seq_stop,
353 .next = obd_device_list_seq_next,
354 .show = obd_device_list_seq_show,
355};
356
357static int obd_device_list_open(struct inode *inode, struct file *file)
358{
d7e09d03
PT
359 struct seq_file *seq;
360 int rc = seq_open(file, &obd_device_list_sops);
361
362 if (rc)
363 return rc;
364
365 seq = file->private_data;
73bb1da6 366 seq->private = PDE_DATA(inode);
d7e09d03
PT
367
368 return 0;
369}
370
371struct file_operations obd_device_list_fops = {
372 .owner = THIS_MODULE,
373 .open = obd_device_list_open,
374 .read = seq_read,
375 .llseek = seq_lseek,
376 .release = seq_release,
377};
378
379int class_procfs_init(void)
380{
5907838a 381 int rc = 0;
d7e09d03
PT
382
383 obd_sysctl_init();
384 proc_lustre_root = lprocfs_register("fs/lustre", NULL,
385 lprocfs_base, NULL);
5907838a
JH
386 if (IS_ERR(proc_lustre_root)) {
387 rc = PTR_ERR(proc_lustre_root);
388 proc_lustre_root = NULL;
389 goto out;
390 }
391
d7e09d03
PT
392 rc = lprocfs_seq_create(proc_lustre_root, "devices", 0444,
393 &obd_device_list_fops, NULL);
5907838a 394out:
d7e09d03
PT
395 if (rc)
396 CERROR("error adding /proc/fs/lustre/devices file\n");
0a3bdb00 397 return 0;
d7e09d03
PT
398}
399
400int class_procfs_clean(void)
401{
d7e09d03
PT
402 if (proc_lustre_root) {
403 lprocfs_remove(&proc_lustre_root);
404 }
0a3bdb00 405 return 0;
d7e09d03 406}