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