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