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