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