Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 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 |
Oleg Drokin | 6a5b99a | 2016-06-14 23:33:40 -0400 | [diff] [blame] | 18 | * http://www.gnu.org/licenses/gpl-2.0.html |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 19 | * |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 20 | * GPL HEADER END |
| 21 | */ |
| 22 | /* |
| 23 | * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. |
| 24 | * Use is subject to license terms. |
| 25 | * |
| 26 | * Copyright (c) 2011, 2012, Intel Corporation. |
| 27 | */ |
| 28 | /* |
| 29 | * This file is part of Lustre, http://www.lustre.org/ |
| 30 | * Lustre is a trademark of Sun Microsystems, Inc. |
| 31 | * |
| 32 | * lustre/obdclass/linux/linux-module.c |
| 33 | * |
| 34 | * Object Devices Class Driver |
| 35 | * These are the only exported functions, they provide some generic |
| 36 | * infrastructure for managing object devices |
| 37 | */ |
| 38 | |
| 39 | #define DEBUG_SUBSYSTEM S_CLASS |
| 40 | |
| 41 | #include <linux/module.h> |
| 42 | #include <linux/errno.h> |
| 43 | #include <linux/kernel.h> |
| 44 | #include <linux/major.h> |
| 45 | #include <linux/sched.h> |
| 46 | #include <linux/lp.h> |
| 47 | #include <linux/slab.h> |
| 48 | #include <linux/ioport.h> |
| 49 | #include <linux/fcntl.h> |
| 50 | #include <linux/delay.h> |
| 51 | #include <linux/skbuff.h> |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 52 | #include <linux/fs.h> |
| 53 | #include <linux/poll.h> |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 54 | #include <linux/list.h> |
| 55 | #include <linux/highmem.h> |
Tapasweni Pathak | 1bcb5bd | 2014-09-29 16:03:12 +0530 | [diff] [blame] | 56 | #include <linux/io.h> |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 57 | #include <asm/ioctls.h> |
Tapasweni Pathak | 1bcb5bd | 2014-09-29 16:03:12 +0530 | [diff] [blame] | 58 | #include <linux/uaccess.h> |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 59 | #include <linux/miscdevice.h> |
| 60 | #include <linux/seq_file.h> |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 61 | #include <linux/kobject.h> |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 62 | |
Greg Kroah-Hartman | 9fdaf8c | 2014-07-11 20:51:16 -0700 | [diff] [blame] | 63 | #include "../../../include/linux/libcfs/libcfs.h" |
| 64 | #include "../../../include/linux/lnet/lnetctl.h" |
Greg Kroah-Hartman | 610f737 | 2014-07-11 22:15:24 -0700 | [diff] [blame] | 65 | #include "../../include/obd_support.h" |
| 66 | #include "../../include/obd_class.h" |
| 67 | #include "../../include/lprocfs_status.h" |
John L. Hammond | 8877d3b | 2016-08-16 16:18:51 -0400 | [diff] [blame] | 68 | #include "../../include/lustre/lustre_ioctl.h" |
Greg Kroah-Hartman | 610f737 | 2014-07-11 22:15:24 -0700 | [diff] [blame] | 69 | #include "../../include/lustre_ver.h" |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 70 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 71 | /* buffer MUST be at least the size of obd_ioctl_hdr */ |
Oleg Drokin | e3e8ff4 | 2016-01-03 12:05:40 -0500 | [diff] [blame] | 72 | int obd_ioctl_getdata(char **buf, int *len, void __user *arg) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 73 | { |
| 74 | struct obd_ioctl_hdr hdr; |
| 75 | struct obd_ioctl_data *data; |
| 76 | int err; |
| 77 | int offset = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 78 | |
Oleg Drokin | e3e8ff4 | 2016-01-03 12:05:40 -0500 | [diff] [blame] | 79 | if (copy_from_user(&hdr, arg, sizeof(hdr))) |
Dan Carpenter | 3b66ea4 | 2014-12-22 10:52:39 +0300 | [diff] [blame] | 80 | return -EFAULT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 81 | |
| 82 | if (hdr.ioc_version != OBD_IOCTL_VERSION) { |
| 83 | CERROR("Version mismatch kernel (%x) vs application (%x)\n", |
| 84 | OBD_IOCTL_VERSION, hdr.ioc_version); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 85 | return -EINVAL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) { |
| 89 | CERROR("User buffer len %d exceeds %d max buffer\n", |
| 90 | hdr.ioc_len, OBD_MAX_IOCTL_BUFFER); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 91 | return -EINVAL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) { |
| 95 | CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 96 | return -EINVAL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* When there are lots of processes calling vmalloc on multi-core |
| 100 | * system, the high lock contention will hurt performance badly, |
| 101 | * obdfilter-survey is an example, which relies on ioctl. So we'd |
Oleg Drokin | 6ba5917 | 2016-02-24 22:00:35 -0500 | [diff] [blame] | 102 | * better avoid vmalloc on ioctl path. LU-66 |
| 103 | */ |
Julia Lawall | 6b0e43d | 2015-06-11 14:02:57 +0200 | [diff] [blame] | 104 | *buf = libcfs_kvzalloc(hdr.ioc_len, GFP_NOFS); |
Oleg Drokin | cce3c2d | 2016-02-16 00:46:55 -0500 | [diff] [blame] | 105 | if (!*buf) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 106 | CERROR("Cannot allocate control buffer of len %d\n", |
| 107 | hdr.ioc_len); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 108 | return -EINVAL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 109 | } |
| 110 | *len = hdr.ioc_len; |
| 111 | data = (struct obd_ioctl_data *)*buf; |
| 112 | |
Oleg Drokin | e3e8ff4 | 2016-01-03 12:05:40 -0500 | [diff] [blame] | 113 | if (copy_from_user(*buf, arg, hdr.ioc_len)) { |
Dan Carpenter | 3b66ea4 | 2014-12-22 10:52:39 +0300 | [diff] [blame] | 114 | err = -EFAULT; |
| 115 | goto free_buf; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 116 | } |
Dan Carpenter | 3b66ea4 | 2014-12-22 10:52:39 +0300 | [diff] [blame] | 117 | if (hdr.ioc_len != data->ioc_len) { |
| 118 | err = -EINVAL; |
| 119 | goto free_buf; |
| 120 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 121 | |
| 122 | if (obd_ioctl_is_invalid(data)) { |
| 123 | CERROR("ioctl not correctly formatted\n"); |
Dan Carpenter | 3b66ea4 | 2014-12-22 10:52:39 +0300 | [diff] [blame] | 124 | err = -EINVAL; |
| 125 | goto free_buf; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | if (data->ioc_inllen1) { |
| 129 | data->ioc_inlbuf1 = &data->ioc_bulk[0]; |
| 130 | offset += cfs_size_round(data->ioc_inllen1); |
| 131 | } |
| 132 | |
| 133 | if (data->ioc_inllen2) { |
| 134 | data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset; |
| 135 | offset += cfs_size_round(data->ioc_inllen2); |
| 136 | } |
| 137 | |
| 138 | if (data->ioc_inllen3) { |
| 139 | data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset; |
| 140 | offset += cfs_size_round(data->ioc_inllen3); |
| 141 | } |
| 142 | |
Bhumika Goyal | 197cb40 | 2016-01-21 00:17:44 +0530 | [diff] [blame] | 143 | if (data->ioc_inllen4) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 144 | data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 145 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 146 | return 0; |
Dan Carpenter | 3b66ea4 | 2014-12-22 10:52:39 +0300 | [diff] [blame] | 147 | |
| 148 | free_buf: |
Julia Lawall | 6b0e43d | 2015-06-11 14:02:57 +0200 | [diff] [blame] | 149 | kvfree(*buf); |
Dan Carpenter | 3b66ea4 | 2014-12-22 10:52:39 +0300 | [diff] [blame] | 150 | return err; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 151 | } |
| 152 | EXPORT_SYMBOL(obd_ioctl_getdata); |
| 153 | |
Oleg Drokin | e3e8ff4 | 2016-01-03 12:05:40 -0500 | [diff] [blame] | 154 | int obd_ioctl_popdata(void __user *arg, void *data, int len) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 155 | { |
| 156 | int err; |
| 157 | |
Liang Zhen | a0d5fce | 2016-03-22 19:03:59 -0400 | [diff] [blame] | 158 | err = copy_to_user(arg, data, len) ? -EFAULT : 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 159 | return err; |
| 160 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 161 | |
| 162 | /* opening /dev/obd */ |
Greg Donald | 5444261 | 2014-10-20 11:18:12 -0500 | [diff] [blame] | 163 | static int obd_class_open(struct inode *inode, struct file *file) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 164 | { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 165 | try_module_get(THIS_MODULE); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 166 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* closing /dev/obd */ |
Greg Donald | 5444261 | 2014-10-20 11:18:12 -0500 | [diff] [blame] | 170 | static int obd_class_release(struct inode *inode, struct file *file) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 171 | { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 172 | module_put(THIS_MODULE); |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 173 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* to control /dev/obd */ |
| 177 | static long obd_class_ioctl(struct file *filp, unsigned int cmd, |
| 178 | unsigned long arg) |
| 179 | { |
| 180 | int err = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 181 | |
| 182 | /* Allow non-root access for OBD_IOC_PING_TARGET - used by lfs check */ |
Peng Tao | 2eb90a7 | 2014-01-22 21:47:37 +0800 | [diff] [blame] | 183 | if (!capable(CFS_CAP_SYS_ADMIN) && (cmd != OBD_IOC_PING_TARGET)) |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 184 | return err = -EACCES; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 185 | if ((cmd & 0xffffff00) == ((int)'T') << 8) /* ignore all tty ioctls */ |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 186 | return err = -ENOTTY; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 187 | |
| 188 | err = class_handle_ioctl(cmd, (unsigned long)arg); |
| 189 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 190 | return err; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* declare character device */ |
Julia Lawall | 495ea0d | 2016-08-28 23:45:11 +0200 | [diff] [blame] | 194 | static const struct file_operations obd_psdev_fops = { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 195 | .owner = THIS_MODULE, |
| 196 | .unlocked_ioctl = obd_class_ioctl, /* unlocked_ioctl */ |
| 197 | .open = obd_class_open, /* open */ |
| 198 | .release = obd_class_release, /* release */ |
| 199 | }; |
| 200 | |
| 201 | /* modules setup */ |
Greg Kroah-Hartman | c0426cf | 2013-07-24 10:21:26 -0700 | [diff] [blame] | 202 | struct miscdevice obd_psdev = { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 203 | .minor = OBD_DEV_MINOR, |
| 204 | .name = OBD_DEV_NAME, |
| 205 | .fops = &obd_psdev_fops, |
| 206 | }; |
| 207 | |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 208 | static ssize_t version_show(struct kobject *kobj, struct attribute *attr, |
| 209 | char *buf) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 210 | { |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 211 | return sprintf(buf, "%s\n", LUSTRE_VERSION_STRING); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 212 | } |
| 213 | |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 214 | static ssize_t pinger_show(struct kobject *kobj, struct attribute *attr, |
| 215 | char *buf) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 216 | { |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 217 | return sprintf(buf, "%s\n", "on"); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 218 | } |
| 219 | |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 220 | static ssize_t health_show(struct kobject *kobj, struct attribute *attr, |
| 221 | char *buf) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 222 | { |
Joe Perches | 8faeebd | 2015-02-21 18:53:28 -0800 | [diff] [blame] | 223 | bool healthy = true; |
| 224 | int i; |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 225 | size_t len = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 226 | |
| 227 | if (libcfs_catastrophe) |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 228 | return sprintf(buf, "LBUG\n"); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 229 | |
| 230 | read_lock(&obd_dev_lock); |
| 231 | for (i = 0; i < class_devno_max(); i++) { |
| 232 | struct obd_device *obd; |
| 233 | |
| 234 | obd = class_num2obd(i); |
Bhumika Goyal | 6ae07f1 | 2016-01-21 00:17:45 +0530 | [diff] [blame] | 235 | if (!obd || !obd->obd_attached || !obd->obd_set_up) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 236 | continue; |
| 237 | |
| 238 | LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC); |
| 239 | if (obd->obd_stopping) |
| 240 | continue; |
| 241 | |
Christoph Jaeger | f9bd9c1 | 2014-03-28 00:21:07 +0100 | [diff] [blame] | 242 | class_incref(obd, __func__, current); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 243 | read_unlock(&obd_dev_lock); |
| 244 | |
Bhumika Goyal | 197cb40 | 2016-01-21 00:17:44 +0530 | [diff] [blame] | 245 | if (obd_health_check(NULL, obd)) |
Joe Perches | 8faeebd | 2015-02-21 18:53:28 -0800 | [diff] [blame] | 246 | healthy = false; |
Christoph Jaeger | f9bd9c1 | 2014-03-28 00:21:07 +0100 | [diff] [blame] | 247 | class_decref(obd, __func__, current); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 248 | read_lock(&obd_dev_lock); |
| 249 | } |
| 250 | read_unlock(&obd_dev_lock); |
| 251 | |
Joe Perches | 8faeebd | 2015-02-21 18:53:28 -0800 | [diff] [blame] | 252 | if (healthy) |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 253 | len = sprintf(buf, "healthy\n"); |
Joe Perches | 8faeebd | 2015-02-21 18:53:28 -0800 | [diff] [blame] | 254 | else |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 255 | len = sprintf(buf, "NOT HEALTHY\n"); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 256 | |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 257 | return len; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 258 | } |
| 259 | |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 260 | static ssize_t jobid_var_show(struct kobject *kobj, struct attribute *attr, |
| 261 | char *buf) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 262 | { |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 263 | return snprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_var); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 264 | } |
| 265 | |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 266 | static ssize_t jobid_var_store(struct kobject *kobj, struct attribute *attr, |
| 267 | const char *buffer, |
| 268 | size_t count) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 269 | { |
| 270 | if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN) |
| 271 | return -EINVAL; |
| 272 | |
| 273 | memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1); |
Oleg Drokin | a1e7e2d | 2014-04-27 13:07:10 -0400 | [diff] [blame] | 274 | |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 275 | memcpy(obd_jobid_var, buffer, count); |
Oleg Drokin | a1e7e2d | 2014-04-27 13:07:10 -0400 | [diff] [blame] | 276 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 277 | /* Trim the trailing '\n' if any */ |
Oleg Drokin | a1e7e2d | 2014-04-27 13:07:10 -0400 | [diff] [blame] | 278 | if (obd_jobid_var[count - 1] == '\n') |
| 279 | obd_jobid_var[count - 1] = 0; |
| 280 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 281 | return count; |
| 282 | } |
| 283 | |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 284 | static ssize_t jobid_name_show(struct kobject *kobj, struct attribute *attr, |
| 285 | char *buf) |
Oleg Drokin | 76133e6 | 2014-04-27 22:21:52 -0400 | [diff] [blame] | 286 | { |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 287 | return snprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_node); |
Oleg Drokin | 76133e6 | 2014-04-27 22:21:52 -0400 | [diff] [blame] | 288 | } |
| 289 | |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 290 | static ssize_t jobid_name_store(struct kobject *kobj, struct attribute *attr, |
| 291 | const char *buffer, |
| 292 | size_t count) |
Oleg Drokin | 76133e6 | 2014-04-27 22:21:52 -0400 | [diff] [blame] | 293 | { |
Henri Doreau | c9fe1f7f | 2016-09-18 16:37:47 -0400 | [diff] [blame] | 294 | if (!count || count > LUSTRE_JOBID_SIZE) |
Oleg Drokin | 76133e6 | 2014-04-27 22:21:52 -0400 | [diff] [blame] | 295 | return -EINVAL; |
| 296 | |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 297 | memcpy(obd_jobid_node, buffer, count); |
Oleg Drokin | 76133e6 | 2014-04-27 22:21:52 -0400 | [diff] [blame] | 298 | |
| 299 | obd_jobid_node[count] = 0; |
| 300 | |
| 301 | /* Trim the trailing '\n' if any */ |
| 302 | if (obd_jobid_node[count - 1] == '\n') |
| 303 | obd_jobid_node[count - 1] = 0; |
| 304 | |
| 305 | return count; |
| 306 | } |
Oleg Drokin | 76133e6 | 2014-04-27 22:21:52 -0400 | [diff] [blame] | 307 | |
Oleg Drokin | 5c8c82f | 2015-05-21 15:32:06 -0400 | [diff] [blame] | 308 | /* Root for /sys/kernel/debug/lustre */ |
| 309 | struct dentry *debugfs_lustre_root; |
| 310 | EXPORT_SYMBOL_GPL(debugfs_lustre_root); |
| 311 | |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 312 | LUSTRE_RO_ATTR(version); |
| 313 | LUSTRE_RO_ATTR(pinger); |
| 314 | LUSTRE_RO_ATTR(health); |
| 315 | LUSTRE_RW_ATTR(jobid_var); |
| 316 | LUSTRE_RW_ATTR(jobid_name); |
| 317 | |
| 318 | static struct attribute *lustre_attrs[] = { |
| 319 | &lustre_attr_version.attr, |
| 320 | &lustre_attr_pinger.attr, |
| 321 | &lustre_attr_health.attr, |
| 322 | &lustre_attr_jobid_name.attr, |
| 323 | &lustre_attr_jobid_var.attr, |
| 324 | NULL, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 325 | }; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 326 | |
| 327 | static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos) |
| 328 | { |
| 329 | if (*pos >= class_devno_max()) |
| 330 | return NULL; |
| 331 | |
| 332 | return pos; |
| 333 | } |
| 334 | |
| 335 | static void obd_device_list_seq_stop(struct seq_file *p, void *v) |
| 336 | { |
| 337 | } |
| 338 | |
| 339 | static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos) |
| 340 | { |
| 341 | ++*pos; |
| 342 | if (*pos >= class_devno_max()) |
| 343 | return NULL; |
| 344 | |
| 345 | return pos; |
| 346 | } |
| 347 | |
| 348 | static int obd_device_list_seq_show(struct seq_file *p, void *v) |
| 349 | { |
| 350 | loff_t index = *(loff_t *)v; |
| 351 | struct obd_device *obd = class_num2obd((int)index); |
| 352 | char *status; |
| 353 | |
Bhumika Goyal | 6ae07f1 | 2016-01-21 00:17:45 +0530 | [diff] [blame] | 354 | if (!obd) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 355 | return 0; |
| 356 | |
| 357 | LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC); |
| 358 | if (obd->obd_stopping) |
| 359 | status = "ST"; |
| 360 | else if (obd->obd_inactive) |
| 361 | status = "IN"; |
| 362 | else if (obd->obd_set_up) |
| 363 | status = "UP"; |
| 364 | else if (obd->obd_attached) |
| 365 | status = "AT"; |
| 366 | else |
| 367 | status = "--"; |
| 368 | |
Joe Perches | 8faeebd | 2015-02-21 18:53:28 -0800 | [diff] [blame] | 369 | seq_printf(p, "%3d %s %s %s %s %d\n", |
| 370 | (int)index, status, obd->obd_type->typ_name, |
| 371 | obd->obd_name, obd->obd_uuid.uuid, |
| 372 | atomic_read(&obd->obd_refcount)); |
| 373 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 374 | } |
| 375 | |
Cihangir Akturk | 83aa2ac | 2015-07-22 15:59:32 +0300 | [diff] [blame] | 376 | static const struct seq_operations obd_device_list_sops = { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 377 | .start = obd_device_list_seq_start, |
| 378 | .stop = obd_device_list_seq_stop, |
| 379 | .next = obd_device_list_seq_next, |
| 380 | .show = obd_device_list_seq_show, |
| 381 | }; |
| 382 | |
| 383 | static int obd_device_list_open(struct inode *inode, struct file *file) |
| 384 | { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 385 | struct seq_file *seq; |
| 386 | int rc = seq_open(file, &obd_device_list_sops); |
| 387 | |
| 388 | if (rc) |
| 389 | return rc; |
| 390 | |
| 391 | seq = file->private_data; |
Oleg Drokin | 4361a04 | 2015-05-21 15:32:07 -0400 | [diff] [blame] | 392 | seq->private = inode->i_private; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
Cihangir Akturk | 83aa2ac | 2015-07-22 15:59:32 +0300 | [diff] [blame] | 397 | static const struct file_operations obd_device_list_fops = { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 398 | .owner = THIS_MODULE, |
| 399 | .open = obd_device_list_open, |
| 400 | .read = seq_read, |
| 401 | .llseek = seq_lseek, |
| 402 | .release = seq_release, |
| 403 | }; |
| 404 | |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 405 | struct kobject *lustre_kobj; |
| 406 | EXPORT_SYMBOL_GPL(lustre_kobj); |
| 407 | |
| 408 | static struct attribute_group lustre_attr_group = { |
| 409 | .attrs = lustre_attrs, |
| 410 | }; |
| 411 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 412 | int class_procfs_init(void) |
| 413 | { |
Oleg Drokin | 3c4872f9 | 2015-07-06 12:48:42 -0400 | [diff] [blame] | 414 | int rc = -ENOMEM; |
Oleg Drokin | 5c8c82f | 2015-05-21 15:32:06 -0400 | [diff] [blame] | 415 | struct dentry *file; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 416 | |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 417 | lustre_kobj = kobject_create_and_add("lustre", fs_kobj); |
Bhumika Goyal | 6ae07f1 | 2016-01-21 00:17:45 +0530 | [diff] [blame] | 418 | if (!lustre_kobj) |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 419 | goto out; |
| 420 | |
| 421 | /* Create the files associated with this kobject */ |
| 422 | rc = sysfs_create_group(lustre_kobj, &lustre_attr_group); |
| 423 | if (rc) { |
| 424 | kobject_put(lustre_kobj); |
| 425 | goto out; |
| 426 | } |
| 427 | |
Oleg Drokin | 5c8c82f | 2015-05-21 15:32:06 -0400 | [diff] [blame] | 428 | debugfs_lustre_root = debugfs_create_dir("lustre", NULL); |
| 429 | if (IS_ERR_OR_NULL(debugfs_lustre_root)) { |
| 430 | rc = debugfs_lustre_root ? PTR_ERR(debugfs_lustre_root) |
| 431 | : -ENOMEM; |
| 432 | debugfs_lustre_root = NULL; |
| 433 | kobject_put(lustre_kobj); |
| 434 | goto out; |
| 435 | } |
| 436 | |
| 437 | file = debugfs_create_file("devices", 0444, debugfs_lustre_root, NULL, |
| 438 | &obd_device_list_fops); |
| 439 | if (IS_ERR_OR_NULL(file)) { |
| 440 | rc = file ? PTR_ERR(file) : -ENOMEM; |
| 441 | kobject_put(lustre_kobj); |
| 442 | goto out; |
| 443 | } |
John L. Hammond | 5907838 | 2013-07-23 00:06:58 +0800 | [diff] [blame] | 444 | out: |
Oleg Drokin | 5c8c82f | 2015-05-21 15:32:06 -0400 | [diff] [blame] | 445 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | int class_procfs_clean(void) |
| 449 | { |
Oleg Drokin | cce3c2d | 2016-02-16 00:46:55 -0500 | [diff] [blame] | 450 | debugfs_remove_recursive(debugfs_lustre_root); |
Oleg Drokin | 5c8c82f | 2015-05-21 15:32:06 -0400 | [diff] [blame] | 451 | |
| 452 | debugfs_lustre_root = NULL; |
| 453 | |
Oleg Drokin | 8b82844 | 2015-05-21 15:31:36 -0400 | [diff] [blame] | 454 | kobject_put(lustre_kobj); |
| 455 | |
Greg Kroah-Hartman | 0a3bdb0 | 2013-08-03 10:35:28 +0800 | [diff] [blame] | 456 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 457 | } |