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