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/fs.h>
57 #include <linux/poll.h>
58 #include <linux/list.h>
59 #include <linux/highmem.h>
60 #include <linux/io.h>
61 #include <asm/ioctls.h>
62 #include <linux/poll.h>
63 #include <linux/uaccess.h>
64 #include <linux/miscdevice.h>
65 #include <linux/seq_file.h>
66 #include <linux/kobject.h>
67
68 #include "../../../include/linux/libcfs/libcfs.h"
69 #include "../../../include/linux/lnet/lnetctl.h"
70 #include "../../include/obd_support.h"
71 #include "../../include/obd_class.h"
72 #include "../../include/lprocfs_status.h"
73 #include "../../include/lustre_ver.h"
74 #include "../../include/lustre/lustre_build_version.h"
75
76 /* buffer MUST be at least the size of obd_ioctl_hdr */
obd_ioctl_getdata(char ** buf,int * len,void * arg)77 int obd_ioctl_getdata(char **buf, int *len, void *arg)
78 {
79 struct obd_ioctl_hdr hdr;
80 struct obd_ioctl_data *data;
81 int err;
82 int offset = 0;
83
84 if (copy_from_user(&hdr, (void *)arg, sizeof(hdr)))
85 return -EFAULT;
86
87 if (hdr.ioc_version != OBD_IOCTL_VERSION) {
88 CERROR("Version mismatch kernel (%x) vs application (%x)\n",
89 OBD_IOCTL_VERSION, hdr.ioc_version);
90 return -EINVAL;
91 }
92
93 if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
94 CERROR("User buffer len %d exceeds %d max buffer\n",
95 hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
96 return -EINVAL;
97 }
98
99 if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
100 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
101 return -EINVAL;
102 }
103
104 /* When there are lots of processes calling vmalloc on multi-core
105 * system, the high lock contention will hurt performance badly,
106 * obdfilter-survey is an example, which relies on ioctl. So we'd
107 * better avoid vmalloc on ioctl path. LU-66 */
108 *buf = libcfs_kvzalloc(hdr.ioc_len, GFP_NOFS);
109 if (*buf == NULL) {
110 CERROR("Cannot allocate control buffer of len %d\n",
111 hdr.ioc_len);
112 return -EINVAL;
113 }
114 *len = hdr.ioc_len;
115 data = (struct obd_ioctl_data *)*buf;
116
117 if (copy_from_user(*buf, (void *)arg, hdr.ioc_len)) {
118 err = -EFAULT;
119 goto free_buf;
120 }
121 if (hdr.ioc_len != data->ioc_len) {
122 err = -EINVAL;
123 goto free_buf;
124 }
125
126 if (obd_ioctl_is_invalid(data)) {
127 CERROR("ioctl not correctly formatted\n");
128 err = -EINVAL;
129 goto free_buf;
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
151 return 0;
152
153 free_buf:
154 kvfree(*buf);
155 return err;
156 }
157 EXPORT_SYMBOL(obd_ioctl_getdata);
158
obd_ioctl_popdata(void * arg,void * data,int len)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 */
obd_class_open(struct inode * inode,struct file * file)171 static int obd_class_open(struct inode *inode, struct file *file)
172 {
173 try_module_get(THIS_MODULE);
174 return 0;
175 }
176
177 /* closing /dev/obd */
obd_class_release(struct inode * inode,struct file * file)178 static int obd_class_release(struct inode *inode, struct file *file)
179 {
180 module_put(THIS_MODULE);
181 return 0;
182 }
183
184 /* to control /dev/obd */
obd_class_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)185 static long obd_class_ioctl(struct file *filp, unsigned int cmd,
186 unsigned long arg)
187 {
188 int err = 0;
189
190 /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */
191 if (!capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET))
192 return err = -EACCES;
193 if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */
194 return err = -ENOTTY;
195
196 err = class_handle_ioctl(cmd, (unsigned long)arg);
197
198 return err;
199 }
200
201 /* declare character device */
202 static struct file_operations obd_psdev_fops = {
203 .owner = THIS_MODULE,
204 .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */
205 .open = obd_class_open, /* open */
206 .release = obd_class_release, /* release */
207 };
208
209 /* modules setup */
210 struct miscdevice obd_psdev = {
211 .minor = OBD_DEV_MINOR,
212 .name = OBD_DEV_NAME,
213 .fops = &obd_psdev_fops,
214 };
215
version_show(struct kobject * kobj,struct attribute * attr,char * buf)216 static ssize_t version_show(struct kobject *kobj, struct attribute *attr,
217 char *buf)
218 {
219 return sprintf(buf, "%s\n", LUSTRE_VERSION_STRING);
220 }
221
pinger_show(struct kobject * kobj,struct attribute * attr,char * buf)222 static ssize_t pinger_show(struct kobject *kobj, struct attribute *attr,
223 char *buf)
224 {
225 return sprintf(buf, "%s\n", "on");
226 }
227
health_show(struct kobject * kobj,struct attribute * attr,char * buf)228 static ssize_t health_show(struct kobject *kobj, struct attribute *attr,
229 char *buf)
230 {
231 bool healthy = true;
232 int i;
233 size_t len = 0;
234
235 if (libcfs_catastrophe)
236 return sprintf(buf, "LBUG\n");
237
238 read_lock(&obd_dev_lock);
239 for (i = 0; i < class_devno_max(); i++) {
240 struct obd_device *obd;
241
242 obd = class_num2obd(i);
243 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
244 continue;
245
246 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
247 if (obd->obd_stopping)
248 continue;
249
250 class_incref(obd, __func__, current);
251 read_unlock(&obd_dev_lock);
252
253 if (obd_health_check(NULL, obd)) {
254 healthy = false;
255 }
256 class_decref(obd, __func__, current);
257 read_lock(&obd_dev_lock);
258 }
259 read_unlock(&obd_dev_lock);
260
261 if (healthy)
262 len = sprintf(buf, "healthy\n");
263 else
264 len = sprintf(buf, "NOT HEALTHY\n");
265
266 return len;
267 }
268
jobid_var_show(struct kobject * kobj,struct attribute * attr,char * buf)269 static ssize_t jobid_var_show(struct kobject *kobj, struct attribute *attr,
270 char *buf)
271 {
272 return snprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_var);
273 }
274
jobid_var_store(struct kobject * kobj,struct attribute * attr,const char * buffer,size_t count)275 static ssize_t jobid_var_store(struct kobject *kobj, struct attribute *attr,
276 const char *buffer,
277 size_t count)
278 {
279 if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
280 return -EINVAL;
281
282 memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
283
284 memcpy(obd_jobid_var, buffer, count);
285
286 /* Trim the trailing '\n' if any */
287 if (obd_jobid_var[count - 1] == '\n')
288 obd_jobid_var[count - 1] = 0;
289
290 return count;
291 }
292
jobid_name_show(struct kobject * kobj,struct attribute * attr,char * buf)293 static ssize_t jobid_name_show(struct kobject *kobj, struct attribute *attr,
294 char *buf)
295 {
296 return snprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_node);
297 }
298
jobid_name_store(struct kobject * kobj,struct attribute * attr,const char * buffer,size_t count)299 static ssize_t jobid_name_store(struct kobject *kobj, struct attribute *attr,
300 const char *buffer,
301 size_t count)
302 {
303 if (!count || count > JOBSTATS_JOBID_SIZE)
304 return -EINVAL;
305
306 memcpy(obd_jobid_node, buffer, count);
307
308 obd_jobid_node[count] = 0;
309
310 /* Trim the trailing '\n' if any */
311 if (obd_jobid_node[count - 1] == '\n')
312 obd_jobid_node[count - 1] = 0;
313
314 return count;
315 }
316
317 /* Root for /sys/kernel/debug/lustre */
318 struct dentry *debugfs_lustre_root;
319 EXPORT_SYMBOL_GPL(debugfs_lustre_root);
320
321 LUSTRE_RO_ATTR(version);
322 LUSTRE_RO_ATTR(pinger);
323 LUSTRE_RO_ATTR(health);
324 LUSTRE_RW_ATTR(jobid_var);
325 LUSTRE_RW_ATTR(jobid_name);
326
327 static struct attribute *lustre_attrs[] = {
328 &lustre_attr_version.attr,
329 &lustre_attr_pinger.attr,
330 &lustre_attr_health.attr,
331 &lustre_attr_jobid_name.attr,
332 &lustre_attr_jobid_var.attr,
333 NULL,
334 };
335
obd_device_list_seq_start(struct seq_file * p,loff_t * pos)336 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
337 {
338 if (*pos >= class_devno_max())
339 return NULL;
340
341 return pos;
342 }
343
obd_device_list_seq_stop(struct seq_file * p,void * v)344 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
345 {
346 }
347
obd_device_list_seq_next(struct seq_file * p,void * v,loff_t * pos)348 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
349 {
350 ++*pos;
351 if (*pos >= class_devno_max())
352 return NULL;
353
354 return pos;
355 }
356
obd_device_list_seq_show(struct seq_file * p,void * v)357 static int obd_device_list_seq_show(struct seq_file *p, void *v)
358 {
359 loff_t index = *(loff_t *)v;
360 struct obd_device *obd = class_num2obd((int)index);
361 char *status;
362
363 if (obd == NULL)
364 return 0;
365
366 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
367 if (obd->obd_stopping)
368 status = "ST";
369 else if (obd->obd_inactive)
370 status = "IN";
371 else if (obd->obd_set_up)
372 status = "UP";
373 else if (obd->obd_attached)
374 status = "AT";
375 else
376 status = "--";
377
378 seq_printf(p, "%3d %s %s %s %s %d\n",
379 (int)index, status, obd->obd_type->typ_name,
380 obd->obd_name, obd->obd_uuid.uuid,
381 atomic_read(&obd->obd_refcount));
382 return 0;
383 }
384
385 static const 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
obd_device_list_open(struct inode * inode,struct file * file)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 = inode->i_private;
402
403 return 0;
404 }
405
406 static const struct file_operations obd_device_list_fops = {
407 .owner = THIS_MODULE,
408 .open = obd_device_list_open,
409 .read = seq_read,
410 .llseek = seq_lseek,
411 .release = seq_release,
412 };
413
414 struct kobject *lustre_kobj;
415 EXPORT_SYMBOL_GPL(lustre_kobj);
416
417 static struct attribute_group lustre_attr_group = {
418 .attrs = lustre_attrs,
419 };
420
class_procfs_init(void)421 int class_procfs_init(void)
422 {
423 int rc = -ENOMEM;
424 struct dentry *file;
425
426 lustre_kobj = kobject_create_and_add("lustre", fs_kobj);
427 if (lustre_kobj == NULL)
428 goto out;
429
430 /* Create the files associated with this kobject */
431 rc = sysfs_create_group(lustre_kobj, &lustre_attr_group);
432 if (rc) {
433 kobject_put(lustre_kobj);
434 goto out;
435 }
436
437 debugfs_lustre_root = debugfs_create_dir("lustre", NULL);
438 if (IS_ERR_OR_NULL(debugfs_lustre_root)) {
439 rc = debugfs_lustre_root ? PTR_ERR(debugfs_lustre_root)
440 : -ENOMEM;
441 debugfs_lustre_root = NULL;
442 kobject_put(lustre_kobj);
443 goto out;
444 }
445
446 file = debugfs_create_file("devices", 0444, debugfs_lustre_root, NULL,
447 &obd_device_list_fops);
448 if (IS_ERR_OR_NULL(file)) {
449 rc = file ? PTR_ERR(file) : -ENOMEM;
450 kobject_put(lustre_kobj);
451 goto out;
452 }
453 out:
454 return rc;
455 }
456
class_procfs_clean(void)457 int class_procfs_clean(void)
458 {
459 if (debugfs_lustre_root != NULL)
460 debugfs_remove_recursive(debugfs_lustre_root);
461
462 debugfs_lustre_root = NULL;
463
464 kobject_put(lustre_kobj);
465
466 return 0;
467 }
468