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