Whamcloud - gitweb
807a01b8f39defd635c63104d5a22c1e73b0890a
[fs/lustre-release.git] / lustre / utils / lctl.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *   Author: Peter J. Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *   Author: Robert Read <rread@clusterfs.com>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  */
25
26
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <errno.h>
34 #include <portals/ptlctl.h>
35 #include "obdctl.h"
36 #include "parser.h"
37
38 static int jt_quit(int argc, char **argv) {
39         Parser_quit(argc, argv);
40         return 0;
41 }
42
43 static int jt_noop(int argc, char **argv) {
44         return 0;
45 }
46
47 static int jt_opt_ignore_errors(int argc, char **argv) {
48         Parser_ignore_errors(1);
49         return 0;
50 }
51
52 /*
53  * XXX Should not belong to here
54  */
55 static int flush_cred_ioctl(char *mp)
56 {
57         int fd, rc;
58
59         fd = open(mp, O_RDONLY);
60         if (fd == -1) {
61                 fprintf(stderr, "flush_cred_ioctl: error open %s: %s\n",
62                         mp, strerror(errno));
63                 return -1;
64         }
65
66         rc = ioctl(fd, LL_IOC_FLUSH_CRED);
67         if (rc == -1) {
68                 fprintf(stderr, "flush_cred_ioctl: error ioctl %s: %s\n",
69                         mp, strerror(errno));
70         }
71
72         close(fd);
73         return rc;
74 }
75
76 static int jt_flush_cred(int argc, char **argv)
77 {
78         FILE   *proc;
79         char    procline[PATH_MAX], *line;
80         int     i, rc = 0;
81
82         /* no args means search all lustre mountpoints */
83         if (argc < 2) {
84                 proc = fopen("/proc/mounts", "r");
85                 if (!proc) {
86                         fprintf(stderr, "%s: can't open /proc/mounts\n",
87                                 jt_cmdname(argv[0]));
88                         return -1;
89                 }
90
91                 while ((line = fgets(procline, PATH_MAX, proc)) != NULL) {
92                         char dev[PATH_MAX];
93                         char mp[PATH_MAX];
94                         char fs[PATH_MAX];
95
96                         if (sscanf(line, "%s %s %s", dev, mp, fs) != 3) {
97                                 fprintf(stderr, "%s: unexpected format in "
98                                                 "/proc/mounts\n",
99                                         jt_cmdname(argv[0]));
100                                 return -1;
101                         }
102
103                         if (strcmp(fs, "lustre") &&
104                             strcmp(fs, "lustre_lite"))
105                                 continue;
106
107                         if (flush_cred_ioctl(mp))
108                                 rc = -1;
109                 }
110         } else {
111                 /* follow the exact flush sequence as supplied */
112                 for (i = 1; i < argc; i++) {
113                         if (flush_cred_ioctl(argv[i]))
114                                 rc = -1;
115                 }
116         }
117
118         return rc;
119 }
120
121 command_t cmdlist[] = {
122         /* Metacommands */
123         {"--device", jt_opt_device, 0,
124          "run <command> after connecting to device <devno>\n"
125          "--device <devno> <command [args ...]>"},
126         {"--threads", jt_opt_threads, 0,
127          "run <threads> separate instances of <command> on device <devno>\n"
128          "--threads <threads> <verbose> <devno> <command [args ...]>"},
129         {"--ignore_errors", jt_opt_ignore_errors, 0,
130          "ignore errors that occur during script processing\n"
131          "--ignore_errors"},
132         {"ignore_errors", jt_opt_ignore_errors, 0,
133          "ignore errors that occur during script processing\n"
134          "ignore_errors"},
135         {"dump", jt_ioc_dump, 0, "usage: dump file, save ioctl buffer to file"},
136
137         /* Network configuration commands */
138         {"==== network config ====", jt_noop, 0, "network config"},
139         {"--net", jt_opt_net, 0, "run <command> after setting network to <net>\n"
140          "usage: --net <tcp/elan/myrinet> <command>"},
141         {"network", jt_ptl_network, 0, "commands that follow apply to net\n"
142          "usage: network <tcp/elan/myrinet>"},
143         {"interface_list", jt_ptl_print_interfaces, 0, "print interface entries\n"
144          "usage: interface_list"},
145         {"add_interface", jt_ptl_add_interface, 0, "add interface entry\n"
146          "usage: add_interface ip [netmask]"},
147         {"del_interface", jt_ptl_del_interface, 0, "del interface entry\n"
148          "usage: del_interface [ip]"},
149         {"peer_list", jt_ptl_print_peers, 0, "print peer entries\n"
150          "usage: peer_list"},
151         {"add_peer", jt_ptl_add_peer, 0, "add an peer entry\n"
152          "usage: add_peer <nid> <host> <port>"},
153         {"del_peer", jt_ptl_del_peer, 0, "remove an peer entry\n"
154          "usage: del_peer [<nid>] [<host>] [ks]"},
155         {"conn_list", jt_ptl_print_connections, 0, "print all the connected remote nid\n"
156          "usage: conn_list"},
157         {"connect", jt_ptl_connect, 0, "connect to a remote nid\n"
158          "usage: connect <host> <port> [iIOC]"},
159         {"disconnect", jt_ptl_disconnect, 0, "disconnect from a remote nid\n"
160          "usage: disconnect [<nid>]"},
161         {"active_tx", jt_ptl_print_active_txs, 0, "print active transmits\n"
162          "usage: active_tx"},
163         {"mynid", jt_ptl_mynid, 0, "inform the socknal of the local nid. "
164          "The nid defaults to hostname for tcp networks and is automatically "
165          "setup for elan/myrinet networks.\n"
166          "usage: mynid [<nid>]"},
167         {"shownid", jt_ptl_shownid, 0, "print the local NID\n"
168          "usage: shownid"},
169         {"add_uuid", jt_lcfg_add_uuid, 0, "associate a UUID with a nid\n"
170          "usage: add_uuid <uuid> <nid> <net_type>"},
171         {"close_uuid", jt_obd_close_uuid, 0, "disconnect a UUID\n"
172          "usage: close_uuid <uuid> <net_type>"},
173         {"del_uuid", jt_lcfg_del_uuid, 0, "delete a UUID association\n"
174          "usage: del_uuid <uuid>"},
175         {"add_route", jt_ptl_add_route, 0,
176          "add an entry to the portals routing table\n"
177          "usage: add_route <gateway> <target> [<target>]"},
178         {"del_route", jt_ptl_del_route, 0,
179          "delete route via gateway to targets from the portals routing table\n"
180          "usage: del_route <gateway> [<target>] [<target>]"},
181         {"set_route", jt_ptl_notify_router, 0,
182          "enable/disable routes via gateway in the portals routing table\n"
183          "usage: set_route <gateway> <up/down> [<time>]"},
184         {"route_list", jt_ptl_print_routes, 0,
185          "print the portals routing table, same as show_route\n"
186          "usage: route_list"},
187         {"show_route", jt_ptl_print_routes, 0,
188          "print the portals routing table, same as route_list\n"
189          "usage: show_route"},
190         {"fail", jt_ptl_fail_nid, 0, "fail/restore communications.\n"
191          "Omitting the count means indefinitely, 0 means restore, "
192          "otherwise fail 'count' messages.\n"
193          "usage: fail nid|_all_ [count]"},
194
195         /* Device selection commands */
196         {"=== device selection ===", jt_noop, 0, "device selection"},
197         {"newdev", jt_lcfg_newdev, 0, "create a new device\n"
198          "usage: newdev"},
199         {"device", jt_obd_device, 0,
200          "set current device to <%name|$name|devno>\n"
201          "usage: device <%name|$name|devno>"},
202         {"cfg_device", jt_lcfg_device, 0,
203          "set current device being configured to <$name>\n"
204          "usage: cfg_device <name>"},
205         {"device_list", jt_obd_list, 0, "show all devices\n"
206          "usage: device_list"},
207         {"dl", jt_obd_list, 0, "show all devices\n"
208          "usage: dl"},
209         {"lustre_build_version", jt_get_version, 0,
210          "print the build version of lustre\n"
211          "usage: lustre_build_version"},
212
213         /* Device configuration commands */
214         {"==== device config =====", jt_noop, 0, "device config"},
215         {"attach", jt_lcfg_attach, 0,
216          "set the type of the current device (with <name> and <uuid>)\n"
217          "usage: attach type [name [uuid]]"},
218         {"setup", jt_lcfg_setup, 0,
219          "type specific device configuration information\n"
220          "usage: setup <args...>"},
221         {"cleanup", jt_obd_cleanup, 0, "cleanup previously setup device\n"
222          "usage: cleanup [force | failover]"},
223         {"detach", jt_obd_detach, 0,
224          "remove driver (and name and uuid) from current device\n"
225          "usage: detach"},
226         {"lov_setup", jt_lcfg_lov_setup, 0, "create a LOV device\n"
227          "usage: lov_setup lov-uuid stripe-count stripe-size offset pattern"},
228         {"lmv_setup", jt_lcfg_lmv_setup, 0,
229          "create an LMV device\n"
230          "usage: lmv_setup lmv-uuid UUID1 [UUID2 ...]"},
231         {"lov_modify_tgts", jt_lcfg_lov_modify_tgts, 0,
232          "add or delete an obd to/from a LOV device\n"
233          "usage: lov_modify_tgts add|del <lov-name> <uuid> <index> <gen>"},
234         {"lov_getconfig", jt_obd_lov_getconfig, 0,
235          "read lov configuration from an mds device\n"
236          "usage: lov_getconfig lov-uuid"},
237         {"record", jt_cfg_record, 0, "record commands that follow in log\n"
238          "usage: record cfg-uuid-name"},
239         {"endrecord", jt_cfg_endrecord, 0, "stop recording\n"
240          "usage: endrecord"},
241         {"parse", jt_cfg_parse, 0, "parse the log of recorded commands for this config\n"
242          "usage: parse config-uuid-name"},
243         {"dump_log", jt_cfg_dump_log, 0, "print log of recorded commands for this config to kernel debug log\n"
244          "usage: dump_log config-uuid-name"},
245         {"clear_log", jt_cfg_clear_log, 0, "delete current config log of recorded commands\n"
246          "usage: clear_log config-name"},
247
248         /* Device operations */
249         {"=== device operations ==", jt_noop, 0, "device operations"},
250         {"probe", jt_obd_connect, 0,
251          "build a connection handle to a device.  This command is used to "
252          "suspend configuration until lctl has ensured that the mds and osc "
253          "services are available.  This is to avoid mount failures in a "
254          "rebooting cluster.\n"
255          "usage: probe [timeout]"},
256         {"close", jt_obd_disconnect, 0,
257          "close the connection handle\n"
258          "usage: close"},
259         {"getattr", jt_obd_getattr, 0,
260          "get attribute for OST object <objid>\n"
261          "usage: getattr <objid>"},
262         {"setattr", jt_obd_setattr, 0,
263          "set mode attribute for OST object <objid>\n"
264          "usage: setattr <objid> <mode>"},
265          {"create", jt_obd_create, 0,
266          "create <num> OST objects (with <mode>)\n"
267          "usage: create [num [mode [verbose [lsm data]]]]"},
268         {"destroy", jt_obd_destroy, 0,
269          "destroy OST object <objid> [num [verbose]]\n"
270          "usage: destroy <num> objects, starting at objid <objid>"},
271         {"test_getattr", jt_obd_test_getattr, 0,
272          "do <num> getattrs (on OST object <objid> (objid+1 on each thread))\n"
273          "usage: test_getattr <num> [verbose [[t]objid]]"},
274         {"test_setattr", jt_obd_test_setattr, 0,
275          "do <num> setattrs (on OST object <objid> (objid+1 on each thread))\n"
276          "usage: test_setattr <num> [verbose [[t]objid]]"},
277         {"test_brw", jt_obd_test_brw, 0,
278          "do <num> bulk read/writes (<npages> per I/O, on OST object <objid>)\n"
279          "usage: test_brw [t]<num> [write [verbose [npages [[t]objid]]]]"},
280         {"get_stripe", jt_obd_get_stripe, 0,
281          "show stripe info for an echo client object\n"
282          "usage: get_stripe objid\n"},
283         {"set_stripe", jt_obd_set_stripe, 0,
284          "set stripe info for an echo client object\n"
285          "usage: set_stripe objid[=width!count[@offset][:id:id...]\n"},
286         {"unset_stripe", jt_obd_unset_stripe, 0,
287          "unset stripe info for an echo client object\n"
288          "usage: unset_stripe objid\n"},
289         {"test_ldlm", jt_obd_test_ldlm, 0,
290          "perform lock manager test\n"
291          "usage: test_ldlm"},
292         {"ldlm_regress_start", jt_obd_ldlm_regress_start, 0,
293          "start lock manager stress test\n"
294          "usage: ldlm_regress_start [numthreads [refheld [numres [numext]]]]"},
295         {"ldlm_regress_stop", jt_obd_ldlm_regress_stop, 0,
296          "stop lock manager stress test (no args)\n"},
297         {"dump_ldlm", jt_obd_dump_ldlm, 0,
298          "dump all lock manager state (no args)"},
299         {"disable_recovery", jt_obd_disable_recovery, 0, "disable recovery on an import\n"},
300         {"enable_recovery", jt_obd_enable_recovery, 0, "enable recovery on an import\n"},
301         {"activate", jt_obd_activate, 0, "activate an import\n"},
302         {"deactivate", jt_obd_deactivate, 0, "deactivate an import\n"},
303         {"recover", jt_obd_recover, 0, "usage: recover [<connection UUID>]"},
304         {"lookup", jt_obd_mdc_lookup, 0, "usage: lookup <directory> <file>"},
305         {"notransno", jt_obd_no_transno, 0,
306          "disable sending of committed-transno updates\n"},
307         {"readonly", jt_obd_set_readonly, 0,
308          "disable writes to the underlying device\n"},
309         {"abort_recovery", jt_obd_abort_recovery, 0,
310          "abort recovery on MDS device\n"},
311         {"root_squash", jt_obd_root_squash, 0,
312          "squash root to 'uid:gid' except client 'nid'\n"
313          "usage: root_squash [uid:gid [nid]]\n"},
314         {"start", jt_obd_start, 0,
315          "setup mds/ost from the llog file\n"
316          "usage: start <profilename>"},
317         {"mount_option", jt_lcfg_mount_option, 0, 
318          "usage: mount_option profile osc_name [mdc_name] [gsc_name] \n"},
319         {"del_mount_option", jt_lcfg_del_mount_option, 0,
320          "usage: del_mount_option profile\n"},
321         {"set_timeout", jt_lcfg_set_timeout, 0,
322          "usage: set_timeout <secs>\n"},
323         {"set_lustre_upcall", jt_lcfg_set_lustre_upcall, 0,
324          "usage: set_lustre_upcall </full/path/to/upcall> \n"},
325         {"add_conn ", jt_lcfg_add_conn, 0,
326          "usage: add_conn <conn_uuid> [priority]\n"},
327         {"del_conn ", jt_lcfg_del_conn, 0,
328          "usage: del_conn <conn_uuid> \n"},
329         {"set_security", jt_lcfg_set_security, 0,
330          "usage: set_security key value\n"},
331         {"lsync", jt_obd_reint_sync, 0,
332          "usage: lsync\n"},  
333         {"cache_on", jt_obd_cache_on, 0,
334          "usage: lsync\n"},  
335         {"cache_off", jt_obd_cache_off, 0,
336          "usage: lsync\n"},  
337 /*
338         {"obd_flush_cred", jt_obd_flush_cred, 0,
339          "usage: obd_flush_cred [all]\n"},
340 */
341         /*snap operations*/
342         {"snap_add", jt_obd_snap_add, 0, 
343          "usage: snap_add <dev_name> <snap_name>\n"}, 
344         /* Llog operations */ 
345         {"llog_catlist", jt_llog_catlist, 0, 
346          "list all catalog logs on current device.\n"
347          "usage: llog_catlist"},
348         {"llog_info", jt_llog_info, 0,
349          "print log header information.\n"
350          "usage: llog_info <$logname|#oid#ogr#ogen>\n"
351          "       oid, ogr and ogen are hexadecimal."},
352         {"llog_print", jt_llog_print, 0,
353          "print log content information.\n"
354          "usage: llog_print <$logname|#oid#ogr#ogen> [from] [to]\n"
355          "       oid, ogr and ogen are hexadecimal.\n"
356          "       print all records from index 1 by default."},
357         {"llog_check", jt_llog_check, 0,
358          "print log content information.\n"
359          "usage: llog_check <$logname|#oid#ogr#ogen> [from] [to]\n"
360          "       oid, ogr and ogen are hexadecimal.\n"
361          "       check all records from index 1 by default."},
362          {"llog_cancel", jt_llog_cancel, 0,
363          "cancel one record in log.\n"
364          "usage: llog_cancel <catalog id|catalog name> <log id> <index>"},
365         {"llog_remove", jt_llog_remove, 0,
366          "remove one log from catalog, erase it from disk.\n"
367          "usage: llog_remove <catalog id|catalog name> <log id>"},
368         
369         /* Misc commands */
370         {"flush_cred", jt_flush_cred, 0,
371          "flush the client side credential.\n"
372          "usage: flush_cred [mountpoint]..."},
373         {"set_crypt", jt_set_lkey_type, 0,
374          "set lustre key management type, only support gsk and mdk\n"
375          "usage: set_crypt [dir] [gsk|mdk]...."},
376         
377         /* Debug commands */
378         {"======== debug =========", jt_noop, 0, "debug"},
379         {"debug_daemon", jt_dbg_debug_daemon, 0,
380          "debug daemon control and dump to a file\n"
381          "usage: debug_daemon {start file [#MB]|stop}"},
382         {"debug_kernel", jt_dbg_debug_kernel, 0,
383          "get debug buffer and dump to a file, same as dk\n"
384          "usage: debug_kernel [file] [raw]"},
385         {"dk", jt_dbg_debug_kernel, 0,
386          "get debug buffer and dump to a file, same as debug_kernel\n"
387          "usage: dk [file] [raw]"},
388         {"debug_file", jt_dbg_debug_file, 0,
389          "read debug buffer from input and dump to output, same as df\n"
390          "usage: debug_file <input> [output] [raw]"},
391         {"df", jt_dbg_debug_file, 0,
392          "read debug buffer from input and dump to output, same as debug_file\n"
393          "usage: df <input> [output] [raw]"},
394         {"clear", jt_dbg_clear_debug_buf, 0, "clear kernel debug buffer\n"
395          "usage: clear"},
396         {"mark", jt_dbg_mark_debug_buf, 0,"insert marker text in kernel debug buffer\n"
397          "usage: mark <text>"},
398         {"filter", jt_dbg_filter, 0, "filter message type\n"
399          "usage: filter <subsystem id/debug mask>"},
400         {"show", jt_dbg_show, 0, "Show specific type of messages\n"
401          "usage: show <subsystem id/debug mask>"},
402         {"debug_list", jt_dbg_list, 0, "list subsystem and debug types\n"
403          "usage: debug_list <subs/types>"},
404         {"modules", jt_dbg_modules, 0,
405          "provide gdb-friendly module information\n"
406          "usage: modules <path>"},
407         {"panic", jt_dbg_panic, 0, "force the kernel to panic\n"
408          "usage: panic"},
409         {"lwt", jt_ptl_lwt, 0,
410          "light-weight tracing\n"
411          "usage: lwt start\n"
412          "       lwt stop [file]"},
413         {"memhog", jt_ptl_memhog, 0,
414          "memory pressure testing\n"
415          "usage: memhog <page count> [<gfp flags>]"},
416                 
417         /* User interface commands */
418         {"======= control ========", jt_noop, 0, "control commands"},
419         {"help", Parser_help, 0, "help"},
420         {"exit", jt_quit, 0, "quit"},
421         {"quit", jt_quit, 0, "quit"},
422         { 0, 0, 0, NULL }
423 };
424
425 int lctl_main(int argc, char **argv)
426 {
427         int rc;
428
429         setlinebuf(stdout);
430
431         ptl_initialize(argc, argv);
432         if (obd_initialize(argc, argv) < 0)
433                 exit(2);
434         if (dbg_initialize(argc, argv) < 0)
435                 exit(3);
436
437         Parser_init("lctl > ", cmdlist);
438
439         if (argc > 1) {
440                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
441         } else {
442                 rc = Parser_commands();
443         }
444
445         obd_finalize(argc, argv);
446         return rc;
447 }
448
449 #if !LIBLUSTRE_TEST
450 int main (int argc, char **argv)
451 {
452         return (lctl_main (argc, argv));
453 }
454 #endif