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