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