Whamcloud - gitweb
LU-521 lnet: Fix endian issue introduced by change 1338
[fs/lustre-release.git] / lnet / selftest / conctl.c
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 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lnet/selftest/conctl.c
35  *
36  * IOC handle in kernel 
37  *
38  * Author: Liang Zhen <liangzhen@clusterfs.com>
39  */
40 #ifdef __KERNEL__
41
42 #include <libcfs/libcfs.h>
43 #include <lnet/lib-lnet.h>
44 #include <lnet/lnetst.h>
45 #include "console.h"
46
47 int
48 lst_session_new_ioctl(lstio_session_new_args_t *args)
49 {
50         char      *name;
51         int        rc;
52
53         if (args->lstio_ses_idp   == NULL || /* address for output sid */
54             args->lstio_ses_key   == 0 || /* no key is specified */
55             args->lstio_ses_namep == NULL || /* session name */
56             args->lstio_ses_nmlen <= 0 ||
57             args->lstio_ses_nmlen > LST_NAME_SIZE)
58                 return -EINVAL;
59
60         LIBCFS_ALLOC(name, args->lstio_ses_nmlen + 1);
61         if (name == NULL)
62                 return -ENOMEM;
63
64         if (cfs_copy_from_user(name,
65                                args->lstio_ses_namep,
66                                args->lstio_ses_nmlen)) {
67                 LIBCFS_FREE(name, args->lstio_ses_nmlen + 1);
68                 return -EFAULT;
69         }
70
71         name[args->lstio_ses_nmlen] = 0;
72
73         rc = lstcon_session_new(name,
74                                 args->lstio_ses_key,
75                                 args->lstio_ses_feats,
76                                 args->lstio_ses_force,
77                                 args->lstio_ses_timeout,
78                                 args->lstio_ses_idp);
79
80         LIBCFS_FREE(name, args->lstio_ses_nmlen + 1);
81         return rc;
82 }
83
84 int
85 lst_session_end_ioctl(lstio_session_end_args_t *args)
86 {
87         if (args->lstio_ses_key != console_session.ses_key)
88                 return -EACCES;
89
90         return lstcon_session_end();
91 }
92
93 int
94 lst_session_info_ioctl(lstio_session_info_args_t *args)
95 {
96         /* no checking of key */
97
98         if (args->lstio_ses_idp   == NULL || /* address for ouput sid */
99             args->lstio_ses_keyp  == NULL || /* address for ouput key */
100             args->lstio_ses_featp  == NULL || /* address for ouput features */
101             args->lstio_ses_ndinfo == NULL || /* address for output ndinfo */
102             args->lstio_ses_namep == NULL || /* address for ouput name */
103             args->lstio_ses_nmlen <= 0 ||
104             args->lstio_ses_nmlen > LST_NAME_SIZE)
105                 return -EINVAL;
106
107         return lstcon_session_info(args->lstio_ses_idp,
108                                    args->lstio_ses_keyp,
109                                    args->lstio_ses_featp,
110                                    args->lstio_ses_ndinfo,
111                                    args->lstio_ses_namep,
112                                    args->lstio_ses_nmlen);
113 }
114
115 int
116 lst_debug_ioctl(lstio_debug_args_t *args)
117 {
118         char   *name   = NULL;
119         int     client = 1;
120         int     rc;
121
122         if (args->lstio_dbg_key != console_session.ses_key)
123                 return -EACCES;
124
125         if (args->lstio_dbg_resultp == NULL)
126                 return -EINVAL;
127
128         if (args->lstio_dbg_namep != NULL && /* name of batch/group */
129             (args->lstio_dbg_nmlen <= 0 ||
130              args->lstio_dbg_nmlen > LST_NAME_SIZE))
131                 return -EINVAL;
132
133         if (args->lstio_dbg_namep != NULL) {
134                 LIBCFS_ALLOC(name, args->lstio_dbg_nmlen + 1);
135                 if (name == NULL)
136                         return -ENOMEM;
137
138                 if (cfs_copy_from_user(name, args->lstio_dbg_namep,
139                                        args->lstio_dbg_nmlen)) {
140                         LIBCFS_FREE(name, args->lstio_dbg_nmlen + 1);
141
142                         return -EFAULT;
143                 }
144
145                 name[args->lstio_dbg_nmlen] = 0;
146         }
147
148         rc = -EINVAL;
149
150         switch (args->lstio_dbg_type) {
151         case LST_OPC_SESSION:
152                 rc = lstcon_session_debug(args->lstio_dbg_timeout,
153                                           args->lstio_dbg_resultp);
154                 break;
155
156         case LST_OPC_BATCHSRV:
157                 client = 0;
158         case LST_OPC_BATCHCLI:
159                 if (name == NULL)
160                         goto out;
161
162                 rc = lstcon_batch_debug(args->lstio_dbg_timeout,
163                                         name, client, args->lstio_dbg_resultp);
164                 break;
165
166         case LST_OPC_GROUP:
167                 if (name == NULL)
168                         goto out;
169
170                 rc = lstcon_group_debug(args->lstio_dbg_timeout,
171                                         name, args->lstio_dbg_resultp);
172                 break;
173
174         case LST_OPC_NODES:
175                 if (args->lstio_dbg_count <= 0 ||
176                     args->lstio_dbg_idsp == NULL)
177                         goto out;
178
179                 rc = lstcon_nodes_debug(args->lstio_dbg_timeout,
180                                         args->lstio_dbg_count,
181                                         args->lstio_dbg_idsp,
182                                         args->lstio_dbg_resultp);
183                 break;
184
185         default:
186                 break;
187         }
188
189 out:
190         if (name != NULL)
191                 LIBCFS_FREE(name, args->lstio_dbg_nmlen + 1);
192
193         return rc;
194 }
195
196 int
197 lst_group_add_ioctl(lstio_group_add_args_t *args)
198 {
199         char           *name;
200         int             rc;
201
202         if (args->lstio_grp_key != console_session.ses_key)
203                 return -EACCES;
204
205         if (args->lstio_grp_namep == NULL||
206             args->lstio_grp_nmlen <= 0 || 
207             args->lstio_grp_nmlen > LST_NAME_SIZE)
208                 return -EINVAL;
209
210         LIBCFS_ALLOC(name, args->lstio_grp_nmlen + 1);
211         if (name == NULL)
212                 return -ENOMEM;
213
214         if (cfs_copy_from_user(name,
215                                args->lstio_grp_namep,
216                                args->lstio_grp_nmlen)) {
217                 LIBCFS_FREE(name, args->lstio_grp_nmlen);
218                 return -EFAULT;
219         }
220
221         name[args->lstio_grp_nmlen] = 0;
222
223         rc = lstcon_group_add(name);
224
225         LIBCFS_FREE(name, args->lstio_grp_nmlen + 1);
226
227         return rc;
228 }
229
230 int
231 lst_group_del_ioctl(lstio_group_del_args_t *args)
232 {
233         int     rc;
234         char   *name;
235
236         if (args->lstio_grp_key != console_session.ses_key)
237                 return -EACCES;
238
239         if (args->lstio_grp_namep == NULL ||
240             args->lstio_grp_nmlen <= 0 ||
241             args->lstio_grp_nmlen > LST_NAME_SIZE)
242                 return -EINVAL;
243
244         LIBCFS_ALLOC(name, args->lstio_grp_nmlen + 1);
245         if (name == NULL)
246                 return -ENOMEM;
247
248         if (cfs_copy_from_user(name,
249                                args->lstio_grp_namep,
250                                args->lstio_grp_nmlen)) {
251                 LIBCFS_FREE(name, args->lstio_grp_nmlen + 1);
252                 return -EFAULT;
253         }
254
255         name[args->lstio_grp_nmlen] = 0;
256
257         rc = lstcon_group_del(name);
258
259         LIBCFS_FREE(name, args->lstio_grp_nmlen + 1);
260
261         return rc;
262 }
263
264 int
265 lst_group_update_ioctl(lstio_group_update_args_t *args)
266 {
267         int     rc;
268         char   *name;
269
270         if (args->lstio_grp_key != console_session.ses_key)
271                 return -EACCES;
272
273         if (args->lstio_grp_resultp == NULL ||
274             args->lstio_grp_namep == NULL ||
275             args->lstio_grp_nmlen <= 0 || 
276             args->lstio_grp_nmlen > LST_NAME_SIZE)
277                 return -EINVAL;
278
279         LIBCFS_ALLOC(name, args->lstio_grp_nmlen + 1);
280         if (name == NULL)
281                 return -ENOMEM;
282
283         if (cfs_copy_from_user(name,
284                            args->lstio_grp_namep,
285                            args->lstio_grp_nmlen)) {
286                 LIBCFS_FREE(name, args->lstio_grp_nmlen + 1);
287                 return -EFAULT;
288         }
289
290         name[args->lstio_grp_nmlen] = 0;
291
292         switch (args->lstio_grp_opc) {
293         case LST_GROUP_CLEAN:
294                 rc = lstcon_group_clean(name, args->lstio_grp_args);
295                 break;
296
297         case LST_GROUP_REFRESH:
298                 rc = lstcon_group_refresh(name, args->lstio_grp_resultp);
299                 break;
300
301         case LST_GROUP_RMND:
302                 if (args->lstio_grp_count  <= 0 ||
303                     args->lstio_grp_idsp == NULL) {
304                         rc = -EINVAL;
305                         break;
306                 }
307                 rc = lstcon_nodes_remove(name, args->lstio_grp_count,
308                                          args->lstio_grp_idsp,
309                                          args->lstio_grp_resultp);
310                 break;
311
312         default:
313                 rc = -EINVAL;
314                 break;
315         }
316
317         LIBCFS_FREE(name, args->lstio_grp_nmlen + 1);
318
319         return rc;
320 }
321
322 int
323 lst_nodes_add_ioctl(lstio_group_nodes_args_t *args)
324 {
325         unsigned feats;
326         int     rc;
327         char   *name;
328
329         if (args->lstio_grp_key != console_session.ses_key)
330                 return -EACCES;
331
332         if (args->lstio_grp_idsp == NULL || /* array of ids */
333             args->lstio_grp_count <= 0 ||
334             args->lstio_grp_resultp == NULL ||
335             args->lstio_grp_featp == NULL ||
336             args->lstio_grp_namep == NULL ||
337             args->lstio_grp_nmlen <= 0 ||
338             args->lstio_grp_nmlen > LST_NAME_SIZE)
339                 return -EINVAL;
340
341         LIBCFS_ALLOC(name, args->lstio_grp_nmlen + 1);
342         if (name == NULL)
343                 return -ENOMEM;
344
345         if (cfs_copy_from_user(name, args->lstio_grp_namep,
346                                args->lstio_grp_nmlen)) {
347                 LIBCFS_FREE(name, args->lstio_grp_nmlen + 1);
348
349                 return -EFAULT;
350         }
351
352         name[args->lstio_grp_nmlen] = 0;
353
354         rc = lstcon_nodes_add(name, args->lstio_grp_count,
355                               args->lstio_grp_idsp, &feats,
356                               args->lstio_grp_resultp);
357
358         LIBCFS_FREE(name, args->lstio_grp_nmlen + 1);
359         if (rc == 0 &&
360             cfs_copy_to_user(args->lstio_grp_featp, &feats, sizeof(feats))) {
361                 return -EINVAL;
362         }
363
364         return rc;
365 }
366
367 int
368 lst_group_list_ioctl(lstio_group_list_args_t *args)
369 {
370         if (args->lstio_grp_key != console_session.ses_key) 
371                 return -EACCES;
372
373         if (args->lstio_grp_idx   < 0 ||
374             args->lstio_grp_namep == NULL ||
375             args->lstio_grp_nmlen <= 0 ||
376             args->lstio_grp_nmlen > LST_NAME_SIZE)
377                 return -EINVAL;
378
379         return lstcon_group_list(args->lstio_grp_idx,
380                               args->lstio_grp_nmlen,
381                               args->lstio_grp_namep);
382 }
383
384 int
385 lst_group_info_ioctl(lstio_group_info_args_t *args)
386 {
387         char           *name;
388         int             ndent;
389         int             index;
390         int             rc;
391
392         if (args->lstio_grp_key != console_session.ses_key)
393                 return -EACCES;
394
395         if (args->lstio_grp_namep == NULL ||
396             args->lstio_grp_nmlen <= 0 ||
397             args->lstio_grp_nmlen > LST_NAME_SIZE)
398                 return -EINVAL;
399
400         if (args->lstio_grp_entp  == NULL && /* output: group entry */
401             args->lstio_grp_dentsp == NULL)  /* output: node entry */
402                 return -EINVAL;
403
404         if (args->lstio_grp_dentsp != NULL) { /* have node entry */
405                 if (args->lstio_grp_idxp == NULL || /* node index */
406                     args->lstio_grp_ndentp == NULL) /* # of node entry */
407                         return -EINVAL;
408
409                 if (cfs_copy_from_user(&ndent, args->lstio_grp_ndentp,
410                                        sizeof(ndent)) ||
411                     cfs_copy_from_user(&index, args->lstio_grp_idxp,
412                                        sizeof(index)))
413                         return -EFAULT;
414
415                 if (ndent <= 0 || index < 0)
416                         return -EINVAL;
417         }
418
419         LIBCFS_ALLOC(name, args->lstio_grp_nmlen + 1);
420         if (name == NULL)
421                 return -ENOMEM;
422
423         if (cfs_copy_from_user(name,
424                                args->lstio_grp_namep,
425                                args->lstio_grp_nmlen)) {
426                 LIBCFS_FREE(name, args->lstio_grp_nmlen + 1);
427                 return -EFAULT;
428         }
429
430         name[args->lstio_grp_nmlen] = 0;
431
432         rc = lstcon_group_info(name, args->lstio_grp_entp,
433                                &index, &ndent, args->lstio_grp_dentsp);
434
435         LIBCFS_FREE(name, args->lstio_grp_nmlen + 1);
436
437         if (rc != 0) 
438                 return rc;
439
440         if (args->lstio_grp_dentsp != NULL && 
441             (cfs_copy_to_user(args->lstio_grp_idxp, &index, sizeof(index)) ||
442              cfs_copy_to_user(args->lstio_grp_ndentp, &ndent, sizeof(ndent))))
443                 rc = -EFAULT;
444
445         return 0;
446 }
447
448 int
449 lst_batch_add_ioctl(lstio_batch_add_args_t *args)
450 {
451         int             rc;
452         char           *name;
453
454         if (args->lstio_bat_key != console_session.ses_key)
455                 return -EACCES;
456
457         if (args->lstio_bat_namep == NULL ||
458             args->lstio_bat_nmlen <= 0 ||
459             args->lstio_bat_nmlen > LST_NAME_SIZE)
460                 return -EINVAL;
461
462         LIBCFS_ALLOC(name, args->lstio_bat_nmlen + 1);
463         if (name == NULL)
464                 return -ENOMEM;
465
466         if (cfs_copy_from_user(name,
467                                args->lstio_bat_namep,
468                                args->lstio_bat_nmlen)) {
469                 LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
470                 return -EFAULT;
471         }
472
473         name[args->lstio_bat_nmlen] = 0;
474
475         rc = lstcon_batch_add(name);
476
477         LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
478
479         return rc;
480 }
481
482 int
483 lst_batch_run_ioctl(lstio_batch_run_args_t *args)
484 {
485         int             rc;
486         char           *name;
487
488         if (args->lstio_bat_key != console_session.ses_key)
489                 return -EACCES;
490
491         if (args->lstio_bat_namep == NULL ||
492             args->lstio_bat_nmlen <= 0 ||
493             args->lstio_bat_nmlen > LST_NAME_SIZE)
494                 return -EINVAL;
495
496         LIBCFS_ALLOC(name, args->lstio_bat_nmlen + 1);
497         if (name == NULL)
498                 return -ENOMEM;
499
500         if (cfs_copy_from_user(name,
501                                args->lstio_bat_namep,
502                                args->lstio_bat_nmlen)) {
503                 LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
504                 return -EFAULT;
505         }
506
507         name[args->lstio_bat_nmlen] = 0;
508
509         rc = lstcon_batch_run(name, args->lstio_bat_timeout,
510                               args->lstio_bat_resultp);
511
512         LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
513
514         return rc;
515 }
516
517 int
518 lst_batch_stop_ioctl(lstio_batch_stop_args_t *args)
519 {
520         int             rc;
521         char           *name;
522
523         if (args->lstio_bat_key != console_session.ses_key)
524                 return -EACCES;
525
526         if (args->lstio_bat_resultp == NULL ||
527             args->lstio_bat_namep == NULL ||
528             args->lstio_bat_nmlen <= 0 ||
529             args->lstio_bat_nmlen > LST_NAME_SIZE)
530                 return -EINVAL;
531
532         LIBCFS_ALLOC(name, args->lstio_bat_nmlen + 1);
533         if (name == NULL)
534                 return -ENOMEM;
535
536         if (cfs_copy_from_user(name,
537                                args->lstio_bat_namep,
538                                args->lstio_bat_nmlen)) {
539                 LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
540                 return -EFAULT;
541         }
542
543         name[args->lstio_bat_nmlen] = 0;
544
545         rc = lstcon_batch_stop(name, args->lstio_bat_force,
546                                args->lstio_bat_resultp);
547
548         LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
549
550         return rc;
551 }
552
553 int
554 lst_batch_query_ioctl(lstio_batch_query_args_t *args)
555 {
556         char   *name;
557         int     rc;
558
559         if (args->lstio_bat_key != console_session.ses_key)
560                 return -EACCES;
561
562         if (args->lstio_bat_resultp == NULL ||
563             args->lstio_bat_namep == NULL ||
564             args->lstio_bat_nmlen <= 0 ||
565             args->lstio_bat_nmlen > LST_NAME_SIZE)
566                 return -EINVAL;
567
568         if (args->lstio_bat_testidx < 0)
569                 return -EINVAL;
570
571         LIBCFS_ALLOC(name, args->lstio_bat_nmlen + 1);
572         if (name == NULL)
573                 return -ENOMEM;
574
575         if (cfs_copy_from_user(name,
576                                args->lstio_bat_namep,
577                                args->lstio_bat_nmlen)) {
578                 LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
579                 return -EFAULT;
580         }
581
582         name[args->lstio_bat_nmlen] = 0;
583
584         rc = lstcon_test_batch_query(name,
585                                      args->lstio_bat_testidx,
586                                      args->lstio_bat_client,
587                                      args->lstio_bat_timeout,
588                                      args->lstio_bat_resultp);
589
590         LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
591
592         return rc;
593 }
594
595 int
596 lst_batch_list_ioctl(lstio_batch_list_args_t *args)
597 {
598         if (args->lstio_bat_key != console_session.ses_key)
599                 return -EACCES;
600
601         if (args->lstio_bat_idx   < 0 ||
602             args->lstio_bat_namep == NULL ||
603             args->lstio_bat_nmlen <= 0 ||
604             args->lstio_bat_nmlen > LST_NAME_SIZE)
605                 return -EINVAL;
606
607         return lstcon_batch_list(args->lstio_bat_idx,
608                               args->lstio_bat_nmlen,
609                               args->lstio_bat_namep);
610 }
611
612 int
613 lst_batch_info_ioctl(lstio_batch_info_args_t *args)
614 {
615         char           *name;
616         int             rc;
617         int             index;
618         int             ndent;
619
620         if (args->lstio_bat_key != console_session.ses_key)
621                 return -EACCES;
622
623         if (args->lstio_bat_namep == NULL || /* batch name */
624             args->lstio_bat_nmlen <= 0 ||
625             args->lstio_bat_nmlen > LST_NAME_SIZE)
626                 return -EINVAL;
627
628         if (args->lstio_bat_entp == NULL && /* output: batch entry */
629             args->lstio_bat_dentsp == NULL) /* output: node entry */
630                 return -EINVAL;
631
632         if (args->lstio_bat_dentsp != NULL) { /* have node entry */
633                 if (args->lstio_bat_idxp == NULL || /* node index */
634                     args->lstio_bat_ndentp == NULL) /* # of node entry */
635                         return -EINVAL;
636
637                 if (cfs_copy_from_user(&index, args->lstio_bat_idxp,
638                                        sizeof(index)) ||
639                     cfs_copy_from_user(&ndent, args->lstio_bat_ndentp,
640                                        sizeof(ndent)))
641                         return -EFAULT;
642
643                 if (ndent <= 0 || index < 0)
644                         return -EINVAL;
645         }
646
647         LIBCFS_ALLOC(name, args->lstio_bat_nmlen + 1);
648         if (name == NULL)
649                 return -ENOMEM;
650
651         if (cfs_copy_from_user(name,
652                                args->lstio_bat_namep, args->lstio_bat_nmlen)) {
653                 LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
654                 return -EFAULT;
655         }
656
657         name[args->lstio_bat_nmlen] = 0;
658
659         rc = lstcon_batch_info(name,
660                             args->lstio_bat_entp, args->lstio_bat_server,
661                             args->lstio_bat_testidx, &index, &ndent,
662                             args->lstio_bat_dentsp);
663
664         LIBCFS_FREE(name, args->lstio_bat_nmlen + 1);
665
666         if (rc != 0)
667                 return rc;
668
669         if (args->lstio_bat_dentsp != NULL && 
670             (cfs_copy_to_user(args->lstio_bat_idxp, &index, sizeof(index)) ||
671              cfs_copy_to_user(args->lstio_bat_ndentp, &ndent, sizeof(ndent))))
672                 rc = -EFAULT;
673
674         return rc;
675 }
676
677 int
678 lst_stat_query_ioctl(lstio_stat_args_t *args)
679 {
680         int             rc;
681         char           *name;
682
683         /* TODO: not finished */
684         if (args->lstio_sta_key != console_session.ses_key)
685                 return -EACCES;
686
687         if (args->lstio_sta_resultp == NULL ||
688             (args->lstio_sta_namep  == NULL &&
689              args->lstio_sta_idsp   == NULL) ||
690             args->lstio_sta_nmlen <= 0 ||
691             args->lstio_sta_nmlen > LST_NAME_SIZE)
692                 return -EINVAL;
693
694         if (args->lstio_sta_idsp != NULL &&
695             args->lstio_sta_count <= 0)
696                 return -EINVAL;
697
698         LIBCFS_ALLOC(name, args->lstio_sta_nmlen + 1);
699         if (name == NULL)
700                 return -ENOMEM;
701
702         if (cfs_copy_from_user(name, args->lstio_sta_namep,
703                                args->lstio_sta_nmlen)) {
704                 LIBCFS_FREE(name, args->lstio_sta_nmlen + 1);
705                 return -EFAULT;
706         }
707
708         if (args->lstio_sta_idsp == NULL) {
709                 rc = lstcon_group_stat(name, args->lstio_sta_timeout,
710                                        args->lstio_sta_resultp);
711         } else {
712                 rc = lstcon_nodes_stat(args->lstio_sta_count,
713                                        args->lstio_sta_idsp,
714                                        args->lstio_sta_timeout,
715                                        args->lstio_sta_resultp);
716         }
717
718         LIBCFS_FREE(name, args->lstio_sta_nmlen + 1);
719
720         return rc;
721 }
722
723 int lst_test_add_ioctl(lstio_test_args_t *args)
724 {
725         char           *name;
726         char           *srcgrp = NULL;
727         char           *dstgrp = NULL;
728         void           *param = NULL;
729         int             ret = 0;
730         int             rc = -ENOMEM;
731
732         if (args->lstio_tes_resultp == NULL ||
733             args->lstio_tes_retp == NULL ||
734             args->lstio_tes_bat_name == NULL || /* no specified batch */
735             args->lstio_tes_bat_nmlen <= 0 ||
736             args->lstio_tes_bat_nmlen > LST_NAME_SIZE ||
737             args->lstio_tes_sgrp_name == NULL || /* no source group */
738             args->lstio_tes_sgrp_nmlen <= 0 ||
739             args->lstio_tes_sgrp_nmlen > LST_NAME_SIZE ||
740             args->lstio_tes_dgrp_name == NULL || /* no target group */
741             args->lstio_tes_dgrp_nmlen <= 0 ||
742             args->lstio_tes_dgrp_nmlen > LST_NAME_SIZE)
743                 return -EINVAL;
744
745         if (args->lstio_tes_loop == 0 || /* negative is infinite */
746             args->lstio_tes_concur <= 0 ||
747             args->lstio_tes_dist <= 0 ||
748             args->lstio_tes_span <= 0)
749                 return -EINVAL;
750
751         /* have parameter, check if parameter length is valid */
752         if (args->lstio_tes_param != NULL &&
753             (args->lstio_tes_param_len <= 0 ||
754              args->lstio_tes_param_len > CFS_PAGE_SIZE - sizeof(lstcon_test_t)))
755                 return -EINVAL;
756
757         LIBCFS_ALLOC(name, args->lstio_tes_bat_nmlen + 1);
758         if (name == NULL)
759                 return rc;
760
761         LIBCFS_ALLOC(srcgrp, args->lstio_tes_sgrp_nmlen + 1);
762         if (srcgrp == NULL) 
763                 goto out;
764
765         LIBCFS_ALLOC(dstgrp, args->lstio_tes_dgrp_nmlen + 1);
766          if (dstgrp == NULL)
767                 goto out;
768
769         if (args->lstio_tes_param != NULL) {
770                 LIBCFS_ALLOC(param, args->lstio_tes_param_len);
771                 if (param == NULL) 
772                         goto out;
773         }
774
775         rc = -EFAULT;
776         if (cfs_copy_from_user(name,
777                               args->lstio_tes_bat_name,
778                               args->lstio_tes_bat_nmlen) ||
779             cfs_copy_from_user(srcgrp,
780                               args->lstio_tes_sgrp_name,
781                               args->lstio_tes_sgrp_nmlen) ||
782             cfs_copy_from_user(dstgrp,
783                               args->lstio_tes_dgrp_name,
784                               args->lstio_tes_dgrp_nmlen) ||
785             cfs_copy_from_user(param, args->lstio_tes_param,
786                               args->lstio_tes_param_len))
787                 goto out;
788
789         rc = lstcon_test_add(name,
790                             args->lstio_tes_type,
791                             args->lstio_tes_loop,
792                             args->lstio_tes_concur,
793                             args->lstio_tes_dist, args->lstio_tes_span,
794                             srcgrp, dstgrp, param, args->lstio_tes_param_len,
795                             &ret, args->lstio_tes_resultp);
796
797         if (ret != 0)
798                 rc = (cfs_copy_to_user(args->lstio_tes_retp, &ret,
799                                        sizeof(ret))) ? -EFAULT : 0;
800 out:
801         if (name != NULL)
802                 LIBCFS_FREE(name, args->lstio_tes_bat_nmlen + 1);
803
804         if (srcgrp != NULL)
805                 LIBCFS_FREE(srcgrp, args->lstio_tes_sgrp_nmlen + 1);
806
807         if (dstgrp != NULL)
808                 LIBCFS_FREE(dstgrp, args->lstio_tes_dgrp_nmlen + 1);
809
810         if (param != NULL)
811                 LIBCFS_FREE(param, args->lstio_tes_param_len);
812
813         return rc;
814 }
815
816 int
817 lstcon_ioctl_entry(unsigned int cmd, struct libcfs_ioctl_data *data)
818 {
819         char   *buf;
820         int     opc = data->ioc_u32[0];
821         int     rc;
822
823         if (cmd != IOC_LIBCFS_LNETST)
824                 return -EINVAL;
825
826         if (data->ioc_plen1 > CFS_PAGE_SIZE)
827                 return -EINVAL;
828
829         LIBCFS_ALLOC(buf, data->ioc_plen1);
830         if (buf == NULL)
831                 return -ENOMEM;
832
833         /* copy in parameter */
834         if (cfs_copy_from_user(buf, data->ioc_pbuf1, data->ioc_plen1)) {
835                 LIBCFS_FREE(buf, data->ioc_plen1);
836                 return -EFAULT;
837         }
838
839         cfs_mutex_lock(&console_session.ses_mutex);
840
841         console_session.ses_laststamp = cfs_time_current_sec();
842
843         if (console_session.ses_shutdown) {
844                 rc = -ESHUTDOWN;
845                 goto out;
846         }
847
848         if (console_session.ses_expired)
849                 lstcon_session_end();
850
851         if (opc != LSTIO_SESSION_NEW &&
852             console_session.ses_state == LST_SESSION_NONE) {
853                 CDEBUG(D_NET, "LST no active session\n");
854                 rc = -ESRCH;
855                 goto out;
856         }
857
858         memset(&console_session.ses_trans_stat, 0, sizeof(lstcon_trans_stat_t));
859
860         switch (opc) {
861                 case LSTIO_SESSION_NEW:
862                         rc = lst_session_new_ioctl((lstio_session_new_args_t *)buf);
863                         break;
864                 case LSTIO_SESSION_END:
865                         rc = lst_session_end_ioctl((lstio_session_end_args_t *)buf);
866                         break;
867                 case LSTIO_SESSION_INFO:
868                         rc = lst_session_info_ioctl((lstio_session_info_args_t *)buf);
869                         break;
870                 case LSTIO_DEBUG:
871                         rc = lst_debug_ioctl((lstio_debug_args_t *)buf);
872                         break;
873                 case LSTIO_GROUP_ADD:
874                         rc = lst_group_add_ioctl((lstio_group_add_args_t *)buf);
875                         break;
876                 case LSTIO_GROUP_DEL:
877                         rc = lst_group_del_ioctl((lstio_group_del_args_t *)buf);
878                         break;
879                 case LSTIO_GROUP_UPDATE:
880                         rc = lst_group_update_ioctl((lstio_group_update_args_t *)buf);
881                         break;
882                 case LSTIO_NODES_ADD:
883                         rc = lst_nodes_add_ioctl((lstio_group_nodes_args_t *)buf);
884                         break;
885                 case LSTIO_GROUP_LIST:
886                         rc = lst_group_list_ioctl((lstio_group_list_args_t *)buf);
887                         break;
888                 case LSTIO_GROUP_INFO:
889                         rc = lst_group_info_ioctl((lstio_group_info_args_t *)buf);
890                         break;
891                 case LSTIO_BATCH_ADD:
892                         rc = lst_batch_add_ioctl((lstio_batch_add_args_t *)buf);
893                         break;
894                 case LSTIO_BATCH_START:
895                         rc = lst_batch_run_ioctl((lstio_batch_run_args_t *)buf);
896                         break;
897                 case LSTIO_BATCH_STOP:
898                         rc = lst_batch_stop_ioctl((lstio_batch_stop_args_t *)buf);
899                         break;
900                 case LSTIO_BATCH_QUERY:
901                         rc = lst_batch_query_ioctl((lstio_batch_query_args_t *)buf);
902                         break;
903                 case LSTIO_BATCH_LIST:
904                         rc = lst_batch_list_ioctl((lstio_batch_list_args_t *)buf);
905                         break;
906                 case LSTIO_BATCH_INFO:
907                         rc = lst_batch_info_ioctl((lstio_batch_info_args_t *)buf);
908                         break;
909                 case LSTIO_TEST_ADD:
910                         rc = lst_test_add_ioctl((lstio_test_args_t *)buf);
911                         break;
912                 case LSTIO_STAT_QUERY:
913                         rc = lst_stat_query_ioctl((lstio_stat_args_t *)buf);
914                         break;
915                 default:
916                         rc = -EINVAL;
917         }
918
919         if (cfs_copy_to_user(data->ioc_pbuf2, &console_session.ses_trans_stat,
920                              sizeof(lstcon_trans_stat_t)))
921                 rc = -EFAULT;
922 out:
923         cfs_mutex_unlock(&console_session.ses_mutex);
924
925         LIBCFS_FREE(buf, data->ioc_plen1);
926
927         return rc;
928 }
929
930 EXPORT_SYMBOL(lstcon_ioctl_entry);
931
932 #endif