Whamcloud - gitweb
LU-9325 obd: replace lprocfs_str_to_s64
[fs/lustre-release.git] / lustre / obdclass / lprocfs_status_server.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2014, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/lprocfs_status_server.c
33  */
34
35 #define DEBUG_SUBSYSTEM S_CLASS
36
37 #include <linux/kobject.h>
38 #include <linux/sysfs.h>
39
40 #include <obd_class.h>
41 #include <lprocfs_status.h>
42 #include <lustre_nodemap.h>
43
44 #ifdef CONFIG_PROC_FS
45
46 int lprocfs_evict_client_open(struct inode *inode, struct file *f)
47 {
48         struct obd_device *obd = PDE_DATA(file_inode(f));
49
50         atomic_inc(&obd->obd_evict_inprogress);
51         return 0;
52 }
53
54 int lprocfs_evict_client_release(struct inode *inode, struct file *f)
55 {
56         struct obd_device *obd = PDE_DATA(file_inode(f));
57
58         atomic_dec(&obd->obd_evict_inprogress);
59         wake_up(&obd->obd_evict_inprogress_waitq);
60
61         return 0;
62 }
63
64 #define BUFLEN (UUID_MAX + 5)
65
66 ssize_t
67 lprocfs_evict_client_seq_write(struct file *file, const char __user *buffer,
68                                size_t count, loff_t *off)
69 {
70         struct seq_file *m = file->private_data;
71         struct obd_device *obd = m->private;
72         char *tmpbuf, *kbuf;
73
74         OBD_ALLOC(kbuf, BUFLEN);
75         if (kbuf == NULL)
76                 return -ENOMEM;
77
78         /*
79          * OBD_ALLOC() will zero kbuf, but we only copy BUFLEN - 1
80          * bytes into kbuf, to ensure that the string is NUL-terminated.
81          * UUID_MAX should include a trailing NUL already.
82          */
83         if (copy_from_user(kbuf, buffer,
84                            min_t(unsigned long, BUFLEN - 1, count))) {
85                 count = -EFAULT;
86                 goto out;
87         }
88         tmpbuf = cfs_firststr(kbuf, min_t(unsigned long, BUFLEN - 1, count));
89         class_incref(obd, __func__, current);
90
91         if (strncmp(tmpbuf, "nid:", 4) == 0)
92                 obd_export_evict_by_nid(obd, tmpbuf + 4);
93         else if (strncmp(tmpbuf, "uuid:", 5) == 0)
94                 obd_export_evict_by_uuid(obd, tmpbuf + 5);
95         else
96                 obd_export_evict_by_uuid(obd, tmpbuf);
97
98         class_decref(obd, __func__, current);
99
100 out:
101         OBD_FREE(kbuf, BUFLEN);
102         return count;
103 }
104 EXPORT_SYMBOL(lprocfs_evict_client_seq_write);
105
106 #undef BUFLEN
107
108 int lprocfs_num_exports_seq_show(struct seq_file *m, void *data)
109 {
110         struct obd_device *obd = data;
111
112         LASSERT(obd != NULL);
113         seq_printf(m, "%u\n", obd->obd_num_exports);
114         return 0;
115 }
116 EXPORT_SYMBOL(lprocfs_num_exports_seq_show);
117
118 static int obd_export_flags2str(struct obd_export *exp, struct seq_file *m)
119 {
120         bool first = true;
121
122         flag2str(exp, failed);
123         flag2str(exp, in_recovery);
124         flag2str(exp, disconnected);
125         flag2str(exp, connecting);
126
127         return 0;
128 }
129
130 static int
131 lprocfs_exp_print_export_seq(struct cfs_hash *hs, struct cfs_hash_bd *bd,
132                              struct hlist_node *hnode, void *cb_data)
133 {
134         struct seq_file         *m = cb_data;
135         struct obd_export       *exp = cfs_hash_object(hs, hnode);
136         struct obd_device       *obd;
137         struct obd_connect_data *ocd;
138
139         LASSERT(exp != NULL);
140         if (exp->exp_nid_stats == NULL)
141                 goto out;
142         obd = exp->exp_obd;
143         ocd = &exp->exp_connect_data;
144
145         seq_printf(m, "%s:\n"
146                    "    name: %s\n"
147                    "    client: %s\n"
148                    "    connect_flags: [ ",
149                    obd_uuid2str(&exp->exp_client_uuid),
150                    obd->obd_name,
151                    obd_export_nid2str(exp));
152         obd_connect_seq_flags2str(m, ocd->ocd_connect_flags,
153                                   ocd->ocd_connect_flags2, ", ");
154         seq_printf(m, " ]\n");
155         obd_connect_data_seqprint(m, ocd);
156         seq_printf(m, "    export_flags: [ ");
157         obd_export_flags2str(exp, m);
158         seq_printf(m, " ]\n");
159
160 out:
161         return 0;
162 }
163
164 /**
165  * RPC connections are composed of an import and an export. Using the
166  * lctl utility we can extract important information about the state.
167  * The lprocfs_exp_export_seq_show routine displays the state information
168  * for the export.
169  *
170  * \param[in] m         seq file
171  * \param[in] data      unused
172  *
173  * \retval              0 on success
174  *
175  * The format of the export state information is like:
176  * a793e354-49c0-aa11-8c4f-a4f2b1a1a92b:
177  *     name: MGS
178  *     client: 10.211.55.10@tcp
179  *     connect_flags: [ version, barrier, adaptive_timeouts, ... ]
180  *     connect_data:
181  *        flags: 0x2000011005002020
182  *        instance: 0
183  *        target_version: 2.10.51.0
184  *        export_flags: [ ... ]
185  *
186  */
187 static int lprocfs_exp_export_seq_show(struct seq_file *m, void *data)
188 {
189         struct nid_stat *stats = m->private;
190         struct obd_device *obd = stats->nid_obd;
191
192         cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
193                               lprocfs_exp_print_export_seq, m);
194         return 0;
195 }
196 LPROC_SEQ_FOPS_RO(lprocfs_exp_export);
197
198 static void lprocfs_free_client_stats(struct nid_stat *client_stat)
199 {
200         CDEBUG(D_CONFIG, "stat %p - data %p/%p\n", client_stat,
201                client_stat->nid_proc, client_stat->nid_stats);
202
203         LASSERTF(atomic_read(&client_stat->nid_exp_ref_count) == 0,
204                  "nid %s:count %d\n", libcfs_nid2str(client_stat->nid),
205                  atomic_read(&client_stat->nid_exp_ref_count));
206
207         if (client_stat->nid_proc)
208                 lprocfs_remove(&client_stat->nid_proc);
209
210         if (client_stat->nid_stats)
211                 lprocfs_free_stats(&client_stat->nid_stats);
212
213         if (client_stat->nid_ldlm_stats)
214                 lprocfs_free_stats(&client_stat->nid_ldlm_stats);
215
216         OBD_FREE_PTR(client_stat);
217         return;
218 }
219
220 void lprocfs_free_per_client_stats(struct obd_device *obd)
221 {
222         struct cfs_hash *hash = obd->obd_nid_stats_hash;
223         struct nid_stat *stat;
224         ENTRY;
225
226         /* we need extra list - because hash_exit called to early */
227         /* not need locking because all clients is died */
228         while (!list_empty(&obd->obd_nid_stats)) {
229                 stat = list_entry(obd->obd_nid_stats.next,
230                                   struct nid_stat, nid_list);
231                 list_del_init(&stat->nid_list);
232                 cfs_hash_del(hash, &stat->nid, &stat->nid_hash);
233                 lprocfs_free_client_stats(stat);
234         }
235         EXIT;
236 }
237 EXPORT_SYMBOL(lprocfs_free_per_client_stats);
238
239 static int
240 lprocfs_exp_print_uuid_seq(struct cfs_hash *hs, struct cfs_hash_bd *bd,
241                            struct hlist_node *hnode, void *cb_data)
242 {
243         struct seq_file *m = cb_data;
244         struct obd_export *exp = cfs_hash_object(hs, hnode);
245
246         if (exp->exp_nid_stats != NULL)
247                 seq_printf(m, "%s\n", obd_uuid2str(&exp->exp_client_uuid));
248         return 0;
249 }
250
251 static int
252 lprocfs_exp_print_nodemap_seq(struct cfs_hash *hs, struct cfs_hash_bd *bd,
253                               struct hlist_node *hnode, void *cb_data)
254 {
255         struct seq_file *m = cb_data;
256         struct obd_export *exp = cfs_hash_object(hs, hnode);
257         struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
258
259         if (nodemap != NULL)
260                 seq_printf(m, "%s\n", nodemap->nm_name);
261         return 0;
262 }
263
264 static int
265 lprocfs_exp_nodemap_seq_show(struct seq_file *m, void *data)
266 {
267         struct nid_stat *stats = m->private;
268         struct obd_device *obd = stats->nid_obd;
269
270         cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
271                               lprocfs_exp_print_nodemap_seq, m);
272         return 0;
273 }
274 LPROC_SEQ_FOPS_RO(lprocfs_exp_nodemap);
275
276 static int lprocfs_exp_uuid_seq_show(struct seq_file *m, void *data)
277 {
278         struct nid_stat *stats = m->private;
279         struct obd_device *obd = stats->nid_obd;
280
281         cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
282                                 lprocfs_exp_print_uuid_seq, m);
283         return 0;
284 }
285 LPROC_SEQ_FOPS_RO(lprocfs_exp_uuid);
286
287 static int
288 lprocfs_exp_print_hash_seq(struct cfs_hash *hs, struct cfs_hash_bd *bd,
289                            struct hlist_node *hnode, void *cb_data)
290
291 {
292         struct seq_file *m = cb_data;
293         struct obd_export *exp = cfs_hash_object(hs, hnode);
294
295         if (exp->exp_lock_hash != NULL) {
296                 cfs_hash_debug_header(m);
297                 cfs_hash_debug_str(hs, m);
298         }
299         return 0;
300 }
301
302 static int lprocfs_exp_hash_seq_show(struct seq_file *m, void *data)
303 {
304         struct nid_stat *stats = m->private;
305         struct obd_device *obd = stats->nid_obd;
306
307         cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
308                                 lprocfs_exp_print_hash_seq, m);
309         return 0;
310 }
311 LPROC_SEQ_FOPS_RO(lprocfs_exp_hash);
312
313 int lprocfs_exp_print_replydata_seq(struct cfs_hash *hs, struct cfs_hash_bd *bd,
314                                     struct hlist_node *hnode, void *cb_data)
315
316 {
317         struct obd_export *exp = cfs_hash_object(hs, hnode);
318         struct seq_file *m = cb_data;
319         struct tg_export_data *ted = &exp->exp_target_data;
320
321         seq_printf(m, "reply_cnt: %d\n"
322                    "reply_max: %d\n"
323                    "reply_released_by_xid: %d\n"
324                    "reply_released_by_tag: %d\n\n",
325                    ted->ted_reply_cnt,
326                    ted->ted_reply_max,
327                    ted->ted_release_xid,
328                    ted->ted_release_tag);
329         return 0;
330 }
331
332 int lprocfs_exp_replydata_seq_show(struct seq_file *m, void *data)
333 {
334         struct nid_stat *stats = m->private;
335         struct obd_device *obd = stats->nid_obd;
336
337         cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
338                                 lprocfs_exp_print_replydata_seq, m);
339         return 0;
340 }
341 LPROC_SEQ_FOPS_RO(lprocfs_exp_replydata);
342
343 int lprocfs_nid_stats_clear_seq_show(struct seq_file *m, void *data)
344 {
345         seq_puts(m, "Write into this file to clear all nid stats and stale nid entries\n");
346         return 0;
347 }
348 EXPORT_SYMBOL(lprocfs_nid_stats_clear_seq_show);
349
350 static int lprocfs_nid_stats_clear_write_cb(void *obj, void *data)
351 {
352         struct nid_stat *stat = obj;
353         ENTRY;
354
355         CDEBUG(D_INFO, "refcnt %d\n", atomic_read(&stat->nid_exp_ref_count));
356         if (atomic_read(&stat->nid_exp_ref_count) == 1) {
357                 /* object has only hash references. */
358                 spin_lock(&stat->nid_obd->obd_nid_lock);
359                 list_move(&stat->nid_list, data);
360                 spin_unlock(&stat->nid_obd->obd_nid_lock);
361                 RETURN(1);
362         }
363         /* we has reference to object - only clear data*/
364         if (stat->nid_stats)
365                 lprocfs_clear_stats(stat->nid_stats);
366
367         RETURN(0);
368 }
369
370 ssize_t
371 lprocfs_nid_stats_clear_seq_write(struct file *file, const char __user *buffer,
372                                         size_t count, loff_t *off)
373 {
374         struct seq_file *m = file->private_data;
375         struct obd_device *obd = m->private;
376         struct nid_stat *client_stat;
377         struct list_head free_list;
378
379         INIT_LIST_HEAD(&free_list);
380         cfs_hash_cond_del(obd->obd_nid_stats_hash,
381                           lprocfs_nid_stats_clear_write_cb, &free_list);
382
383         while (!list_empty(&free_list)) {
384                 client_stat = list_entry(free_list.next, struct nid_stat,
385                                          nid_list);
386                 list_del_init(&client_stat->nid_list);
387                 lprocfs_free_client_stats(client_stat);
388         }
389         return count;
390 }
391 EXPORT_SYMBOL(lprocfs_nid_stats_clear_seq_write);
392
393 int lprocfs_exp_setup(struct obd_export *exp, lnet_nid_t *nid)
394 {
395         struct nid_stat *new_stat, *old_stat;
396         struct obd_device *obd = NULL;
397         struct proc_dir_entry *entry;
398         char nidstr[LNET_NIDSTR_SIZE];
399         int rc = 0;
400         ENTRY;
401
402         if (!exp || !exp->exp_obd || !exp->exp_obd->obd_proc_exports_entry ||
403             !exp->exp_obd->obd_nid_stats_hash)
404                 RETURN(-EINVAL);
405
406         /* not test against zero because eric say:
407          * You may only test nid against another nid, or LNET_NID_ANY.
408          * Anything else is nonsense.*/
409         if (nid == NULL || *nid == LNET_NID_ANY)
410                 RETURN(-EALREADY);
411
412         libcfs_nid2str_r(*nid, nidstr, sizeof(nidstr));
413
414         spin_lock(&exp->exp_lock);
415         if (exp->exp_nid_stats != NULL) {
416                 spin_unlock(&exp->exp_lock);
417                 RETURN(-EALREADY);
418         }
419         spin_unlock(&exp->exp_lock);
420
421         obd = exp->exp_obd;
422
423         CDEBUG(D_CONFIG, "using hash %p\n", obd->obd_nid_stats_hash);
424
425         OBD_ALLOC_PTR(new_stat);
426         if (new_stat == NULL)
427                 RETURN(-ENOMEM);
428
429         new_stat->nid     = *nid;
430         new_stat->nid_obd = exp->exp_obd;
431         /* we need set default refcount to 1 to balance obd_disconnect */
432         atomic_set(&new_stat->nid_exp_ref_count, 1);
433
434         old_stat = cfs_hash_findadd_unique(obd->obd_nid_stats_hash,
435                                            nid, &new_stat->nid_hash);
436         CDEBUG(D_INFO, "Found stats %p for nid %s - ref %d\n",
437                old_stat, nidstr, atomic_read(&old_stat->nid_exp_ref_count));
438
439         /* Return -EALREADY here so that we know that the /proc
440          * entry already has been created */
441         if (old_stat != new_stat) {
442                 spin_lock(&exp->exp_lock);
443                 if (exp->exp_nid_stats) {
444                         LASSERT(exp->exp_nid_stats == old_stat);
445                         nidstat_putref(exp->exp_nid_stats);
446                 }
447                 exp->exp_nid_stats = old_stat;
448                 spin_unlock(&exp->exp_lock);
449                 GOTO(destroy_new, rc = -EALREADY);
450         }
451         /* not found - create */
452         new_stat->nid_proc = lprocfs_register(nidstr,
453                                               obd->obd_proc_exports_entry,
454                                               NULL, NULL);
455
456         if (IS_ERR(new_stat->nid_proc)) {
457                 rc = PTR_ERR(new_stat->nid_proc);
458                 new_stat->nid_proc = NULL;
459                 CERROR("%s: cannot create proc entry for export %s: rc = %d\n",
460                        obd->obd_name, nidstr, rc);
461                 GOTO(destroy_new_ns, rc);
462         }
463
464         entry = lprocfs_add_simple(new_stat->nid_proc, "nodemap", new_stat,
465                                    &lprocfs_exp_nodemap_fops);
466         if (IS_ERR(entry)) {
467                 rc = PTR_ERR(entry);
468                 CWARN("%s: error adding the nodemap file: rc = %d\n",
469                       obd->obd_name, rc);
470                 GOTO(destroy_new_ns, rc);
471         }
472
473         entry = lprocfs_add_simple(new_stat->nid_proc, "uuid", new_stat,
474                                    &lprocfs_exp_uuid_fops);
475         if (IS_ERR(entry)) {
476                 rc = PTR_ERR(entry);
477                 CWARN("%s: error adding the NID stats file: rc = %d\n",
478                       obd->obd_name, rc);
479                 GOTO(destroy_new_ns, rc);
480         }
481
482         entry = lprocfs_add_simple(new_stat->nid_proc, "hash", new_stat,
483                                    &lprocfs_exp_hash_fops);
484         if (IS_ERR(entry)) {
485                 rc = PTR_ERR(entry);
486                 CWARN("%s: error adding the hash file: rc = %d\n",
487                       obd->obd_name, rc);
488                 GOTO(destroy_new_ns, rc);
489         }
490
491         entry = lprocfs_add_simple(new_stat->nid_proc, "export",
492                                    new_stat, &lprocfs_exp_export_fops);
493         if (IS_ERR(entry)) {
494                 rc = PTR_ERR(entry);
495                 CWARN("%s: error adding the export file: rc = %d\n",
496                       obd->obd_name, rc);
497                 GOTO(destroy_new_ns, rc);
498         }
499
500         entry = lprocfs_add_simple(new_stat->nid_proc, "reply_data", new_stat,
501                                    &lprocfs_exp_replydata_fops);
502         if (IS_ERR(entry)) {
503                 rc = PTR_ERR(entry);
504                 CWARN("%s: error adding the reply_data file: rc = %d\n",
505                       obd->obd_name, rc);
506                 GOTO(destroy_new_ns, rc);
507         }
508
509         spin_lock(&exp->exp_lock);
510         exp->exp_nid_stats = new_stat;
511         spin_unlock(&exp->exp_lock);
512
513         /* protect competitive add to list, not need locking on destroy */
514         spin_lock(&obd->obd_nid_lock);
515         list_add(&new_stat->nid_list, &obd->obd_nid_stats);
516         spin_unlock(&obd->obd_nid_lock);
517
518         RETURN(0);
519
520 destroy_new_ns:
521         if (new_stat->nid_proc != NULL)
522                 lprocfs_remove(&new_stat->nid_proc);
523         cfs_hash_del(obd->obd_nid_stats_hash, nid, &new_stat->nid_hash);
524
525 destroy_new:
526         nidstat_putref(new_stat);
527         OBD_FREE_PTR(new_stat);
528         RETURN(rc);
529 }
530 EXPORT_SYMBOL(lprocfs_exp_setup);
531
532 int lprocfs_exp_cleanup(struct obd_export *exp)
533 {
534         struct nid_stat *stat = exp->exp_nid_stats;
535
536         if (!stat || !exp->exp_obd)
537                 RETURN(0);
538
539         nidstat_putref(exp->exp_nid_stats);
540         exp->exp_nid_stats = NULL;
541
542         return 0;
543 }
544
545 #define LPROCFS_OBD_OP_INIT(base, stats, op)                    \
546 do {                                                            \
547         unsigned int coffset = base + OBD_COUNTER_OFFSET(op);   \
548         LASSERT(coffset < stats->ls_num);                       \
549         lprocfs_counter_init(stats, coffset, 0, #op, "reqs");   \
550 } while (0)
551
552 void lprocfs_init_ops_stats(int num_private_stats, struct lprocfs_stats *stats)
553 {
554         LPROCFS_OBD_OP_INIT(num_private_stats, stats, iocontrol);
555         LPROCFS_OBD_OP_INIT(num_private_stats, stats, get_info);
556         LPROCFS_OBD_OP_INIT(num_private_stats, stats, set_info_async);
557         LPROCFS_OBD_OP_INIT(num_private_stats, stats, setup);
558         LPROCFS_OBD_OP_INIT(num_private_stats, stats, precleanup);
559         LPROCFS_OBD_OP_INIT(num_private_stats, stats, cleanup);
560         LPROCFS_OBD_OP_INIT(num_private_stats, stats, process_config);
561         LPROCFS_OBD_OP_INIT(num_private_stats, stats, postrecov);
562         LPROCFS_OBD_OP_INIT(num_private_stats, stats, add_conn);
563         LPROCFS_OBD_OP_INIT(num_private_stats, stats, del_conn);
564         LPROCFS_OBD_OP_INIT(num_private_stats, stats, connect);
565         LPROCFS_OBD_OP_INIT(num_private_stats, stats, reconnect);
566         LPROCFS_OBD_OP_INIT(num_private_stats, stats, disconnect);
567         LPROCFS_OBD_OP_INIT(num_private_stats, stats, fid_init);
568         LPROCFS_OBD_OP_INIT(num_private_stats, stats, fid_fini);
569         LPROCFS_OBD_OP_INIT(num_private_stats, stats, fid_alloc);
570         LPROCFS_OBD_OP_INIT(num_private_stats, stats, statfs);
571         LPROCFS_OBD_OP_INIT(num_private_stats, stats, statfs_async);
572         LPROCFS_OBD_OP_INIT(num_private_stats, stats, create);
573         LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy);
574         LPROCFS_OBD_OP_INIT(num_private_stats, stats, setattr);
575         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr);
576         LPROCFS_OBD_OP_INIT(num_private_stats, stats, preprw);
577         LPROCFS_OBD_OP_INIT(num_private_stats, stats, commitrw);
578         LPROCFS_OBD_OP_INIT(num_private_stats, stats, init_export);
579         LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy_export);
580         LPROCFS_OBD_OP_INIT(num_private_stats, stats, import_event);
581         LPROCFS_OBD_OP_INIT(num_private_stats, stats, notify);
582         LPROCFS_OBD_OP_INIT(num_private_stats, stats, health_check);
583         LPROCFS_OBD_OP_INIT(num_private_stats, stats, get_uuid);
584         LPROCFS_OBD_OP_INIT(num_private_stats, stats, quotactl);
585         LPROCFS_OBD_OP_INIT(num_private_stats, stats, ping);
586         LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_new);
587         LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_rem);
588         LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_add);
589         LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_del);
590         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getref);
591         LPROCFS_OBD_OP_INIT(num_private_stats, stats, putref);
592
593         CLASSERT(NUM_OBD_STATS == OBD_COUNTER_OFFSET(putref) + 1);
594 }
595 EXPORT_SYMBOL(lprocfs_init_ops_stats);
596
597 int lprocfs_alloc_obd_stats(struct obd_device *obd, unsigned num_private_stats)
598 {
599         struct lprocfs_stats *stats;
600         unsigned int num_stats;
601         int rc, i;
602
603         LASSERT(obd->obd_stats == NULL);
604         LASSERT(obd->obd_proc_entry != NULL);
605         LASSERT(obd->obd_cntr_base == 0);
606
607         num_stats = NUM_OBD_STATS + num_private_stats;
608         stats = lprocfs_alloc_stats(num_stats, 0);
609         if (stats == NULL)
610                 return -ENOMEM;
611
612         lprocfs_init_ops_stats(num_private_stats, stats);
613
614         for (i = num_private_stats; i < num_stats; i++) {
615                 /* If this LBUGs, it is likely that an obd
616                  * operation was added to struct obd_ops in
617                  * <obd.h>, and that the corresponding line item
618                  * LPROCFS_OBD_OP_INIT(.., .., opname)
619                  * is missing from the list above. */
620                 LASSERTF(stats->ls_cnt_header[i].lc_name != NULL,
621                          "Missing obd_stat initializer obd_op "
622                          "operation at offset %d.\n", i - num_private_stats);
623         }
624         rc = lprocfs_register_stats(obd->obd_proc_entry, "stats", stats);
625         if (rc < 0) {
626                 lprocfs_free_stats(&stats);
627         } else {
628                 obd->obd_stats  = stats;
629                 obd->obd_cntr_base = num_private_stats;
630         }
631         return rc;
632 }
633 EXPORT_SYMBOL(lprocfs_alloc_obd_stats);
634
635 void lprocfs_free_obd_stats(struct obd_device *obd)
636 {
637         if (obd->obd_stats)
638                 lprocfs_free_stats(&obd->obd_stats);
639 }
640 EXPORT_SYMBOL(lprocfs_free_obd_stats);
641
642 int lprocfs_hash_seq_show(struct seq_file *m, void *data)
643 {
644         struct obd_device *obd = m->private;
645
646         if (obd == NULL)
647                 return 0;
648
649         cfs_hash_debug_header(m);
650         cfs_hash_debug_str(obd->obd_uuid_hash, m);
651         cfs_hash_debug_str(obd->obd_nid_hash, m);
652         cfs_hash_debug_str(obd->obd_nid_stats_hash, m);
653         return 0;
654 }
655 EXPORT_SYMBOL(lprocfs_hash_seq_show);
656
657 int lprocfs_recovery_status_seq_show(struct seq_file *m, void *data)
658 {
659         struct obd_device *obd = m->private;
660         struct target_distribute_txn_data *tdtd;
661
662         LASSERT(obd != NULL);
663
664         seq_printf(m, "status: ");
665         if (obd->obd_max_recoverable_clients == 0) {
666                 seq_printf(m, "INACTIVE\n");
667                 goto out;
668         }
669
670         /* sampled unlocked, but really... */
671         if (obd->obd_recovering == 0) {
672                 seq_printf(m, "COMPLETE\n");
673                 seq_printf(m, "recovery_start: %lld\n",
674                            (s64)obd->obd_recovery_start);
675                 seq_printf(m, "recovery_duration: %lld\n",
676                            obd->obd_recovery_end ?
677                            obd->obd_recovery_end - obd->obd_recovery_start :
678                            ktime_get_real_seconds() - obd->obd_recovery_start);
679                 /* Number of clients that have completed recovery */
680                 seq_printf(m, "completed_clients: %d/%d\n",
681                            obd->obd_max_recoverable_clients -
682                            obd->obd_stale_clients,
683                            obd->obd_max_recoverable_clients);
684                 seq_printf(m, "replayed_requests: %d\n",
685                            obd->obd_replayed_requests);
686                 seq_printf(m, "last_transno: %lld\n",
687                            obd->obd_next_recovery_transno - 1);
688                 seq_printf(m, "VBR: %s\n", obd->obd_version_recov ?
689                            "ENABLED" : "DISABLED");
690                 seq_printf(m, "IR: %s\n", obd->obd_no_ir ?
691                            "DISABLED" : "ENABLED");
692                 goto out;
693         }
694
695         tdtd = obd->u.obt.obt_lut->lut_tdtd;
696         if (tdtd && tdtd->tdtd_show_update_logs_retrievers) {
697                 char *buf;
698                 int size = 0;
699                 int count = 0;
700
701                 buf = tdtd->tdtd_show_update_logs_retrievers(
702                         tdtd->tdtd_show_retrievers_cbdata,
703                         &size, &count);
704                 if (count > 0) {
705                         seq_printf(m, "WAITING\n");
706                         seq_printf(m, "non-ready MDTs: %s\n",
707                                    buf ? buf : "unknown (not enough RAM)");
708                         seq_printf(m, "recovery_start: %lld\n",
709                                    (s64)obd->obd_recovery_start);
710                         seq_printf(m, "time_waited: %lld\n",
711                                    (s64)(ktime_get_real_seconds() -
712                                          obd->obd_recovery_start));
713                 }
714
715                 if (buf != NULL)
716                         OBD_FREE(buf, size);
717
718                 if (likely(count > 0))
719                         goto out;
720         }
721
722         /* recovery won't start until the clients connect */
723         if (obd->obd_recovery_start == 0) {
724                 seq_printf(m, "WAITING_FOR_CLIENTS\n");
725                 goto out;
726         }
727
728         seq_printf(m, "RECOVERING\n");
729         seq_printf(m, "recovery_start: %lld\n", (s64)obd->obd_recovery_start);
730         seq_printf(m, "time_remaining: %lld\n",
731                    ktime_get_real_seconds() >=
732                    obd->obd_recovery_start +
733                    obd->obd_recovery_timeout ? 0 :
734                    (s64)(obd->obd_recovery_start +
735                          obd->obd_recovery_timeout -
736                          ktime_get_real_seconds()));
737         seq_printf(m, "connected_clients: %d/%d\n",
738                    atomic_read(&obd->obd_connected_clients),
739                    obd->obd_max_recoverable_clients);
740         /* Number of clients that have completed recovery */
741         seq_printf(m, "req_replay_clients: %d\n",
742                    atomic_read(&obd->obd_req_replay_clients));
743         seq_printf(m, "lock_repay_clients: %d\n",
744                    atomic_read(&obd->obd_lock_replay_clients));
745         seq_printf(m, "completed_clients: %d\n",
746                    atomic_read(&obd->obd_connected_clients) -
747                    atomic_read(&obd->obd_lock_replay_clients));
748         seq_printf(m, "evicted_clients: %d\n", obd->obd_stale_clients);
749         seq_printf(m, "replayed_requests: %d\n", obd->obd_replayed_requests);
750         seq_printf(m, "queued_requests: %d\n",
751                    obd->obd_requests_queued_for_recovery);
752         seq_printf(m, "next_transno: %lld\n",
753                    obd->obd_next_recovery_transno);
754 out:
755         return 0;
756 }
757 EXPORT_SYMBOL(lprocfs_recovery_status_seq_show);
758
759 int lprocfs_ir_factor_seq_show(struct seq_file *m, void *data)
760 {
761         struct obd_device *obd = m->private;
762
763         LASSERT(obd != NULL);
764         seq_printf(m, "%d\n", obd->obd_recovery_ir_factor);
765         return 0;
766 }
767 EXPORT_SYMBOL(lprocfs_ir_factor_seq_show);
768
769 ssize_t
770 lprocfs_ir_factor_seq_write(struct file *file, const char __user *buffer,
771                             size_t count, loff_t *off)
772 {
773         struct seq_file *m = file->private_data;
774         struct obd_device *obd = m->private;
775         int val;
776         int rc;
777
778         LASSERT(obd != NULL);
779         rc = kstrtoint_from_user(buffer, count, 10, &val);
780         if (rc)
781                 return rc;
782
783         if (val < OBD_IR_FACTOR_MIN || val > OBD_IR_FACTOR_MAX)
784                 return -EINVAL;
785
786         obd->obd_recovery_ir_factor = val;
787         return count;
788 }
789 EXPORT_SYMBOL(lprocfs_ir_factor_seq_write);
790
791 int lprocfs_checksum_dump_seq_show(struct seq_file *m, void *data)
792 {
793         struct obd_device *obd = m->private;
794
795         LASSERT(obd != NULL);
796         seq_printf(m, "%d\n", obd->obd_checksum_dump);
797         return 0;
798 }
799 EXPORT_SYMBOL(lprocfs_checksum_dump_seq_show);
800
801 ssize_t
802 lprocfs_checksum_dump_seq_write(struct file *file, const char __user *buffer,
803                             size_t count, loff_t *off)
804 {
805         struct seq_file *m = file->private_data;
806         struct obd_device *obd = m->private;
807         bool val;
808         int rc;
809
810         LASSERT(obd != NULL);
811         rc = kstrtobool_from_user(buffer, count, &val);
812         if (rc)
813                 return rc;
814
815         obd->obd_checksum_dump = val;
816         return count;
817 }
818 EXPORT_SYMBOL(lprocfs_checksum_dump_seq_write);
819
820 int lprocfs_recovery_time_soft_seq_show(struct seq_file *m, void *data)
821 {
822         struct obd_device *obd = m->private;
823
824         LASSERT(obd != NULL);
825         seq_printf(m, "%llu\n", obd->obd_recovery_timeout);
826         return 0;
827 }
828 EXPORT_SYMBOL(lprocfs_recovery_time_soft_seq_show);
829
830 ssize_t
831 lprocfs_recovery_time_soft_seq_write(struct file *file,
832                                      const char __user *buffer,
833                                      size_t count, loff_t *off)
834 {
835         struct seq_file *m = file->private_data;
836         struct obd_device *obd = m->private;
837         unsigned int val;
838         int rc;
839
840         LASSERT(obd != NULL);
841         rc = kstrtouint_from_user(buffer, count, 0, &val);
842         if (rc)
843                 return rc;
844
845         obd->obd_recovery_timeout = val;
846         return count;
847 }
848 EXPORT_SYMBOL(lprocfs_recovery_time_soft_seq_write);
849
850 int lprocfs_recovery_time_hard_seq_show(struct seq_file *m, void *data)
851 {
852         struct obd_device *obd = m->private;
853
854         LASSERT(obd != NULL);
855         seq_printf(m, "%lld\n", obd->obd_recovery_time_hard);
856         return 0;
857 }
858 EXPORT_SYMBOL(lprocfs_recovery_time_hard_seq_show);
859
860 ssize_t
861 lprocfs_recovery_time_hard_seq_write(struct file *file,
862                                      const char __user *buffer,
863                                      size_t count, loff_t *off)
864 {
865         struct seq_file *m = file->private_data;
866         struct obd_device *obd = m->private;
867         unsigned int val;
868         int rc;
869
870         LASSERT(obd != NULL);
871         rc = kstrtouint_from_user(buffer, count, 0, &val);
872         if (rc)
873                 return rc;
874
875         obd->obd_recovery_time_hard = val;
876         return count;
877 }
878 EXPORT_SYMBOL(lprocfs_recovery_time_hard_seq_write);
879
880 int lprocfs_target_instance_seq_show(struct seq_file *m, void *data)
881 {
882         struct obd_device *obd = m->private;
883         struct obd_device_target *target = &obd->u.obt;
884
885         LASSERT(obd != NULL);
886         LASSERT(target->obt_magic == OBT_MAGIC);
887         seq_printf(m, "%u\n", obd->u.obt.obt_instance);
888         return 0;
889 }
890 EXPORT_SYMBOL(lprocfs_target_instance_seq_show);
891
892 #endif /* CONFIG_PROC_FS*/