Whamcloud - gitweb
LU-10391 lnet: use large nids in struct lnet_event
[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  *
31  * lustre/obdclass/lprocfs_status_server.c
32  */
33
34 #define DEBUG_SUBSYSTEM S_CLASS
35
36 #include <linux/kobject.h>
37 #include <linux/sysfs.h>
38
39 #include <obd_class.h>
40 #include <lprocfs_status.h>
41 #include <lustre_nodemap.h>
42
43 #define MAX_STRING_SIZE 128
44
45 struct dentry *ldebugfs_add_symlink(const char *name, const char *target,
46                                     const char *format, ...)
47 {
48         struct dentry *entry = NULL;
49         struct dentry *parent;
50         struct qstr dname;
51         va_list ap;
52         char *dest;
53
54         if (!target || !format)
55                 return NULL;
56
57         dname.name = target;
58         dname.len = strlen(dname.name);
59         dname.hash = ll_full_name_hash(debugfs_lustre_root,
60                                        dname.name, dname.len);
61         parent = d_lookup(debugfs_lustre_root, &dname);
62         if (!parent)
63                 return NULL;
64
65         OBD_ALLOC_WAIT(dest, MAX_STRING_SIZE + 1);
66         if (!dest)
67                 goto no_entry;
68
69         va_start(ap, format);
70         vsnprintf(dest, MAX_STRING_SIZE, format, ap);
71         va_end(ap);
72
73         entry = debugfs_create_symlink(name, parent, dest);
74
75         OBD_FREE(dest, MAX_STRING_SIZE + 1);
76 no_entry:
77         dput(parent);
78         return entry;
79 }
80 EXPORT_SYMBOL(ldebugfs_add_symlink);
81
82 #ifdef CONFIG_PROC_FS
83
84 int lprocfs_evict_client_open(struct inode *inode, struct file *f)
85 {
86         struct obd_device *obd = PDE_DATA(file_inode(f));
87
88         atomic_inc(&obd->obd_evict_inprogress);
89         return 0;
90 }
91
92 int lprocfs_evict_client_release(struct inode *inode, struct file *f)
93 {
94         struct obd_device *obd = PDE_DATA(file_inode(f));
95
96         atomic_dec(&obd->obd_evict_inprogress);
97         wake_up(&obd->obd_evict_inprogress_waitq);
98
99         return 0;
100 }
101
102 #define BUFLEN (UUID_MAX + 5)
103
104 ssize_t
105 lprocfs_evict_client_seq_write(struct file *file, const char __user *buffer,
106                                size_t count, loff_t *off)
107 {
108         struct seq_file *m = file->private_data;
109         struct obd_device *obd = m->private;
110         char *tmpbuf, *kbuf;
111
112         OBD_ALLOC(kbuf, BUFLEN);
113         if (kbuf == NULL)
114                 return -ENOMEM;
115
116         /*
117          * OBD_ALLOC() will zero kbuf, but we only copy BUFLEN - 1
118          * bytes into kbuf, to ensure that the string is NUL-terminated.
119          * UUID_MAX should include a trailing NUL already.
120          */
121         if (copy_from_user(kbuf, buffer,
122                            min_t(unsigned long, BUFLEN - 1, count))) {
123                 count = -EFAULT;
124                 goto out;
125         }
126         tmpbuf = skip_spaces(kbuf);
127         tmpbuf = strsep(&tmpbuf, " \t\n\f\v\r");
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 ssize_t grant_check_threshold_show(struct kobject *kobj, struct attribute *attr,
158                                    char *buf)
159 {
160         struct obd_device *obd = container_of(kobj, struct obd_device,
161                                               obd_kset.kobj);
162
163         return scnprintf(buf, PAGE_SIZE, "%d\n",
164                          obd->obd_grant_check_threshold);
165 }
166 EXPORT_SYMBOL(grant_check_threshold_show);
167
168 ssize_t grant_check_threshold_store(struct kobject *kobj,
169                                     struct attribute *attr,
170                                     const char *buffer, size_t count)
171 {
172         struct obd_device *obd = container_of(kobj, struct obd_device,
173                                               obd_kset.kobj);
174         int val;
175         int rc;
176
177         rc = kstrtoint(buffer, 10, &val);
178         if (rc)
179                 return rc;
180
181         if (val < 0)
182                 return -EINVAL;
183         obd->obd_grant_check_threshold = val;
184         return count;
185 }
186 EXPORT_SYMBOL(grant_check_threshold_store);
187
188 static int obd_export_flags2str(struct obd_export *exp, struct seq_file *m)
189 {
190         bool first = true;
191
192         flag2str(exp, failed);
193         flag2str(exp, in_recovery);
194         flag2str(exp, disconnected);
195         flag2str(exp, connecting);
196         flag2str(exp, no_recovery);
197
198         return 0;
199 }
200
201 static int
202 lprocfs_exp_print_export_seq(struct obd_export *exp, void *cb_data)
203 {
204         struct seq_file         *m = cb_data;
205         struct obd_device       *obd;
206         struct obd_connect_data *ocd;
207
208         LASSERT(exp != NULL);
209         if (exp->exp_nid_stats == NULL)
210                 goto out;
211         obd = exp->exp_obd;
212         ocd = &exp->exp_connect_data;
213
214         seq_printf(m, "%s:\n"
215                    "    name: %s\n"
216                    "    client: %s\n"
217                    "    connect_flags: [ ",
218                    obd_uuid2str(&exp->exp_client_uuid),
219                    obd->obd_name,
220                    obd_export_nid2str(exp));
221         obd_connect_seq_flags2str(m, ocd->ocd_connect_flags,
222                                   ocd->ocd_connect_flags2, ", ");
223         seq_printf(m, " ]\n");
224         obd_connect_data_seqprint(m, ocd);
225         seq_printf(m, "    export_flags: [ ");
226         obd_export_flags2str(exp, m);
227         seq_printf(m, " ]\n");
228
229         if (obd->obd_type &&
230             strcmp(obd->obd_type->typ_name, "obdfilter") == 0) {
231                 struct filter_export_data *fed = &exp->exp_filter_data;
232
233                 seq_printf(m, "    grant:\n");
234                 seq_printf(m, "       granted: %ld\n",
235                         fed->fed_ted.ted_grant);
236                 seq_printf(m, "       dirty: %ld\n",
237                         fed->fed_ted.ted_dirty);
238                 seq_printf(m, "       pending: %ld\n",
239                         fed->fed_ted.ted_pending);
240         }
241
242 out:
243         return 0;
244 }
245
246 /**
247  * RPC connections are composed of an import and an export. Using the
248  * lctl utility we can extract important information about the state.
249  * The lprocfs_exp_export_seq_show routine displays the state information
250  * for the export.
251  *
252  * \param[in] m         seq file
253  * \param[in] data      unused
254  *
255  * \retval              0 on success
256  *
257  * The format of the export state information is like:
258  * a793e354-49c0-aa11-8c4f-a4f2b1a1a92b:
259  *     name: MGS
260  *     client: 10.211.55.10@tcp
261  *     connect_flags: [ version, barrier, adaptive_timeouts, ... ]
262  *     connect_data:
263  *        flags: 0x2000011005002020
264  *        instance: 0
265  *        target_version: 2.10.51.0
266  *        export_flags: [ ... ]
267  *
268  */
269 static int lprocfs_exp_export_seq_show(struct seq_file *m, void *data)
270 {
271         struct nid_stat *stats = m->private;
272
273         return obd_nid_export_for_each(stats->nid_obd, &stats->nid,
274                                        lprocfs_exp_print_export_seq, m);
275 }
276 LPROC_SEQ_FOPS_RO(lprocfs_exp_export);
277
278 static void lprocfs_free_client_stats(struct nid_stat *client_stat)
279 {
280         CDEBUG(D_CONFIG, "stat %p - data %p/%p\n", client_stat,
281                client_stat->nid_proc, client_stat->nid_stats);
282
283         LASSERTF(atomic_read(&client_stat->nid_exp_ref_count) == 0,
284                  "nid %s:count %d\n", libcfs_nidstr(&client_stat->nid),
285                  atomic_read(&client_stat->nid_exp_ref_count));
286
287         if (client_stat->nid_proc)
288                 lprocfs_remove(&client_stat->nid_proc);
289
290         if (client_stat->nid_stats)
291                 lprocfs_free_stats(&client_stat->nid_stats);
292
293         if (client_stat->nid_ldlm_stats)
294                 lprocfs_free_stats(&client_stat->nid_ldlm_stats);
295
296         OBD_FREE_PTR(client_stat);
297 }
298
299 void lprocfs_free_per_client_stats(struct obd_device *obd)
300 {
301         struct cfs_hash *hash = obd->obd_nid_stats_hash;
302         struct nid_stat *stat;
303         ENTRY;
304
305         /* we need extra list - because hash_exit called to early */
306         /* not need locking because all clients is died */
307         while (!list_empty(&obd->obd_nid_stats)) {
308                 stat = list_entry(obd->obd_nid_stats.next,
309                                   struct nid_stat, nid_list);
310                 list_del_init(&stat->nid_list);
311                 cfs_hash_del(hash, &stat->nid, &stat->nid_hash);
312                 lprocfs_free_client_stats(stat);
313         }
314         EXIT;
315 }
316 EXPORT_SYMBOL(lprocfs_free_per_client_stats);
317
318 static int
319 lprocfs_exp_print_nodemap_seq(struct obd_export *exp, void *cb_data)
320 {
321         struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
322         struct seq_file *m = cb_data;
323
324         if (nodemap)
325                 seq_printf(m, "%s\n", nodemap->nm_name);
326         return 0;
327 }
328
329 static int
330 lprocfs_exp_nodemap_seq_show(struct seq_file *m, void *data)
331 {
332         struct nid_stat *stats = m->private;
333
334         return obd_nid_export_for_each(stats->nid_obd, &stats->nid,
335                                        lprocfs_exp_print_nodemap_seq, m);
336 }
337 LPROC_SEQ_FOPS_RO(lprocfs_exp_nodemap);
338
339 static int
340 lprocfs_exp_print_uuid_seq(struct obd_export *exp, void *cb_data)
341 {
342         struct seq_file *m = cb_data;
343
344         if (exp->exp_nid_stats)
345                 seq_printf(m, "%s\n", obd_uuid2str(&exp->exp_client_uuid));
346         return 0;
347 }
348
349 static int lprocfs_exp_uuid_seq_show(struct seq_file *m, void *data)
350 {
351         struct nid_stat *stats = m->private;
352
353         return obd_nid_export_for_each(stats->nid_obd, &stats->nid,
354                                        lprocfs_exp_print_uuid_seq, m);
355 }
356 LPROC_SEQ_FOPS_RO(lprocfs_exp_uuid);
357
358 #define HASH_NAME_LEN   16
359
360 static void ldebugfs_rhash_seq_show(const char *name, struct rhashtable *ht,
361                                     struct seq_file *m)
362 {
363         unsigned int max_size = ht->p.max_size ? ht->p.max_size : UINT_MAX;
364         struct bucket_table *tbl;
365         int dist[8] = { 0, };
366         int maxdep = 0;
367         int i;
368
369         rcu_read_lock();
370         tbl = rht_dereference(ht->tbl, ht);
371         for (i = 0; i < tbl->size; i++) {
372                 struct rhash_head *pos;
373                 int count = 0;
374
375                 rht_for_each(pos, tbl, i)
376                         count++;
377
378                 if (count)
379                         maxdep = max(maxdep, count);
380
381                 dist[min(fls(count), 7)]++;
382         }
383
384         seq_printf(m, "%-*s %5d %5d %10u %d.%03d 0.300 0.750 0x%03x %7d %7d %7d ",
385                    HASH_NAME_LEN, name, tbl->size, ht->p.min_size, max_size,
386                    atomic_read(&ht->nelems) / tbl->size,
387                    atomic_read(&ht->nelems) * 1000 / tbl->size,
388                    ht->p.automatic_shrinking, 0,
389                    atomic_read(&ht->nelems), maxdep);
390         rcu_read_unlock();
391
392         for (i = 0; i < 8; i++)
393                 seq_printf(m, "%d%c",  dist[i], (i == 7) ? '\n' : '/');
394 }
395
396 static int
397 lprocfs_exp_print_hash_seq(struct obd_export *exp, void *cb_data)
398
399 {
400         struct obd_device *obd = exp->exp_obd;
401         struct seq_file *m = cb_data;
402
403         if (exp->exp_lock_hash != NULL) {
404                 seq_printf(m, "%-*s   cur   min        max theta t-min t-max flags rehash   count distribution\n",
405                            HASH_NAME_LEN, "name");
406                 ldebugfs_rhash_seq_show("NID_HASH", &obd->obd_nid_hash.ht, m);
407         }
408         return 0;
409 }
410
411 static int lprocfs_exp_hash_seq_show(struct seq_file *m, void *data)
412 {
413         struct nid_stat *stats = m->private;
414
415         return obd_nid_export_for_each(stats->nid_obd, &stats->nid,
416                                        lprocfs_exp_print_hash_seq, m);
417 }
418 LPROC_SEQ_FOPS_RO(lprocfs_exp_hash);
419
420 int lprocfs_exp_print_replydata_seq(struct obd_export *exp, void *cb_data)
421
422 {
423         struct seq_file *m = cb_data;
424         struct tg_export_data *ted = &exp->exp_target_data;
425
426         seq_printf(m, "reply_cnt: %d\n"
427                    "reply_max: %d\n"
428                    "reply_released_by_xid: %d\n"
429                    "reply_released_by_tag: %d\n\n",
430                    ted->ted_reply_cnt,
431                    ted->ted_reply_max,
432                    ted->ted_release_xid,
433                    ted->ted_release_tag);
434         return 0;
435 }
436
437 int lprocfs_exp_replydata_seq_show(struct seq_file *m, void *data)
438 {
439         struct nid_stat *stats = m->private;
440
441         return obd_nid_export_for_each(stats->nid_obd, &stats->nid,
442                                        lprocfs_exp_print_replydata_seq, m);
443 }
444 LPROC_SEQ_FOPS_RO(lprocfs_exp_replydata);
445
446 int lprocfs_exp_print_fmd_count_seq(struct obd_export *exp, void *cb_data)
447 {
448         struct seq_file *m = cb_data;
449         struct tg_export_data *ted = &exp->exp_target_data;
450
451         seq_printf(m, "%d\n", ted->ted_fmd_count);
452
453         return 0;
454 }
455
456 int lprocfs_exp_fmd_count_seq_show(struct seq_file *m, void *data)
457 {
458         struct nid_stat *stats = m->private;
459
460         return obd_nid_export_for_each(stats->nid_obd, &stats->nid,
461                                        lprocfs_exp_print_fmd_count_seq, m);
462 }
463 LPROC_SEQ_FOPS_RO(lprocfs_exp_fmd_count);
464
465 int lprocfs_nid_stats_clear_seq_show(struct seq_file *m, void *data)
466 {
467         seq_puts(m, "Write into this file to clear all nid stats and stale nid entries\n");
468         return 0;
469 }
470 EXPORT_SYMBOL(lprocfs_nid_stats_clear_seq_show);
471
472 static int lprocfs_nid_stats_clear_write_cb(void *obj, void *data)
473 {
474         struct nid_stat *stat = obj;
475         ENTRY;
476
477         CDEBUG(D_INFO, "refcnt %d\n", atomic_read(&stat->nid_exp_ref_count));
478         if (atomic_read(&stat->nid_exp_ref_count) == 1) {
479                 /* object has only hash references. */
480                 spin_lock(&stat->nid_obd->obd_nid_lock);
481                 list_move(&stat->nid_list, data);
482                 spin_unlock(&stat->nid_obd->obd_nid_lock);
483                 RETURN(1);
484         }
485         /* we has reference to object - only clear data*/
486         if (stat->nid_stats)
487                 lprocfs_clear_stats(stat->nid_stats);
488
489         RETURN(0);
490 }
491
492 ssize_t
493 lprocfs_nid_stats_clear_seq_write(struct file *file, const char __user *buffer,
494                                         size_t count, loff_t *off)
495 {
496         struct seq_file *m = file->private_data;
497         struct obd_device *obd = m->private;
498         struct nid_stat *client_stat;
499         LIST_HEAD(free_list);
500
501         cfs_hash_cond_del(obd->obd_nid_stats_hash,
502                           lprocfs_nid_stats_clear_write_cb, &free_list);
503
504         while (!list_empty(&free_list)) {
505                 client_stat = list_entry(free_list.next, struct nid_stat,
506                                          nid_list);
507                 list_del_init(&client_stat->nid_list);
508                 lprocfs_free_client_stats(client_stat);
509         }
510         return count;
511 }
512 EXPORT_SYMBOL(lprocfs_nid_stats_clear_seq_write);
513
514 int lprocfs_exp_setup(struct obd_export *exp, lnet_nid_t *nid)
515 {
516         struct nid_stat *new_stat, *old_stat;
517         struct obd_device *obd = NULL;
518         struct proc_dir_entry *entry;
519         char nidstr[LNET_NIDSTR_SIZE];
520         int rc = 0;
521         ENTRY;
522
523         if (!exp || !exp->exp_obd || !exp->exp_obd->obd_proc_exports_entry ||
524             !exp->exp_obd->obd_nid_stats_hash)
525                 RETURN(-EINVAL);
526
527         /* not test against zero because eric say:
528          * You may only test nid against another nid, or LNET_NID_ANY.
529          * Anything else is nonsense.*/
530         if (nid == NULL || *nid == LNET_NID_ANY)
531                 RETURN(-EALREADY);
532
533         libcfs_nid2str_r(*nid, nidstr, sizeof(nidstr));
534
535         spin_lock(&exp->exp_lock);
536         if (exp->exp_nid_stats != NULL) {
537                 spin_unlock(&exp->exp_lock);
538                 RETURN(-EALREADY);
539         }
540         spin_unlock(&exp->exp_lock);
541
542         obd = exp->exp_obd;
543
544         CDEBUG(D_CONFIG, "using hash %p\n", obd->obd_nid_stats_hash);
545
546         OBD_ALLOC_PTR(new_stat);
547         if (new_stat == NULL)
548                 RETURN(-ENOMEM);
549
550         lnet_nid4_to_nid(*nid, &new_stat->nid);
551         new_stat->nid_obd = exp->exp_obd;
552         /* we need set default refcount to 1 to balance obd_disconnect */
553         atomic_set(&new_stat->nid_exp_ref_count, 1);
554
555         old_stat = cfs_hash_findadd_unique(obd->obd_nid_stats_hash,
556                                            &new_stat->nid,
557                                            &new_stat->nid_hash);
558         CDEBUG(D_INFO, "Found stats %p for nid %s - ref %d\n",
559                old_stat, nidstr, atomic_read(&old_stat->nid_exp_ref_count));
560
561         /* Return -EALREADY here so that we know that the /proc
562          * entry already has been created */
563         if (old_stat != new_stat) {
564                 spin_lock(&exp->exp_lock);
565                 if (exp->exp_nid_stats) {
566                         LASSERT(exp->exp_nid_stats == old_stat);
567                         nidstat_putref(exp->exp_nid_stats);
568                 }
569                 exp->exp_nid_stats = old_stat;
570                 spin_unlock(&exp->exp_lock);
571                 GOTO(destroy_new, rc = -EALREADY);
572         }
573         /* not found - create */
574         new_stat->nid_proc = lprocfs_register(nidstr,
575                                               obd->obd_proc_exports_entry,
576                                               NULL, NULL);
577
578         if (IS_ERR(new_stat->nid_proc)) {
579                 rc = PTR_ERR(new_stat->nid_proc);
580                 new_stat->nid_proc = NULL;
581                 CERROR("%s: cannot create proc entry for export %s: rc = %d\n",
582                        obd->obd_name, nidstr, rc);
583                 GOTO(destroy_new_ns, rc);
584         }
585
586         entry = lprocfs_add_simple(new_stat->nid_proc, "nodemap", new_stat,
587                                    &lprocfs_exp_nodemap_fops);
588         if (IS_ERR(entry)) {
589                 rc = PTR_ERR(entry);
590                 CWARN("%s: error adding the nodemap 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, "uuid", new_stat,
596                                    &lprocfs_exp_uuid_fops);
597         if (IS_ERR(entry)) {
598                 rc = PTR_ERR(entry);
599                 CWARN("%s: error adding the NID stats 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, "hash", new_stat,
605                                    &lprocfs_exp_hash_fops);
606         if (IS_ERR(entry)) {
607                 rc = PTR_ERR(entry);
608                 CWARN("%s: error adding the hash 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, "export",
614                                    new_stat, &lprocfs_exp_export_fops);
615         if (IS_ERR(entry)) {
616                 rc = PTR_ERR(entry);
617                 CWARN("%s: error adding the export file: rc = %d\n",
618                       obd->obd_name, rc);
619                 GOTO(destroy_new_ns, rc);
620         }
621
622         entry = lprocfs_add_simple(new_stat->nid_proc, "reply_data", new_stat,
623                                    &lprocfs_exp_replydata_fops);
624         if (IS_ERR(entry)) {
625                 rc = PTR_ERR(entry);
626                 CWARN("%s: error adding the reply_data file: rc = %d\n",
627                       obd->obd_name, rc);
628                 GOTO(destroy_new_ns, rc);
629         }
630
631         entry = lprocfs_add_simple(new_stat->nid_proc, "fmd_count", new_stat,
632                                    &lprocfs_exp_fmd_count_fops);
633         if (IS_ERR(entry)) {
634                 rc = PTR_ERR(entry);
635                 CWARN("%s: error adding the fmd_count file: rc = %d\n",
636                       obd->obd_name, rc);
637                 GOTO(destroy_new_ns, rc);
638         }
639
640         spin_lock(&exp->exp_lock);
641         exp->exp_nid_stats = new_stat;
642         spin_unlock(&exp->exp_lock);
643
644         /* protect competitive add to list, not need locking on destroy */
645         spin_lock(&obd->obd_nid_lock);
646         list_add(&new_stat->nid_list, &obd->obd_nid_stats);
647         spin_unlock(&obd->obd_nid_lock);
648
649         RETURN(0);
650
651 destroy_new_ns:
652         if (new_stat->nid_proc != NULL)
653                 lprocfs_remove(&new_stat->nid_proc);
654         cfs_hash_del(obd->obd_nid_stats_hash, &new_stat->nid,
655                      &new_stat->nid_hash);
656
657 destroy_new:
658         nidstat_putref(new_stat);
659         OBD_FREE_PTR(new_stat);
660         RETURN(rc);
661 }
662 EXPORT_SYMBOL(lprocfs_exp_setup);
663
664 int lprocfs_exp_cleanup(struct obd_export *exp)
665 {
666         struct nid_stat *stat = exp->exp_nid_stats;
667
668         if (!stat || !exp->exp_obd)
669                 RETURN(0);
670
671         nidstat_putref(exp->exp_nid_stats);
672         exp->exp_nid_stats = NULL;
673
674         return 0;
675 }
676
677 int lprocfs_alloc_obd_stats(struct obd_device *obd, unsigned int num_stats)
678 {
679         struct lprocfs_stats *stats;
680         int rc;
681
682         LASSERT(obd->obd_stats == NULL);
683         LASSERT(obd->obd_proc_entry != NULL);
684
685         stats = lprocfs_alloc_stats(num_stats, 0);
686         if (stats == NULL)
687                 return -ENOMEM;
688
689         rc = lprocfs_register_stats(obd->obd_proc_entry, "stats", stats);
690         if (rc < 0)
691                 lprocfs_free_stats(&stats);
692         else
693                 obd->obd_stats = stats;
694
695         return rc;
696 }
697 EXPORT_SYMBOL(lprocfs_alloc_obd_stats);
698
699 void lprocfs_free_obd_stats(struct obd_device *obd)
700 {
701         if (obd->obd_stats)
702                 lprocfs_free_stats(&obd->obd_stats);
703 }
704 EXPORT_SYMBOL(lprocfs_free_obd_stats);
705
706 static void display_brw_stats(struct seq_file *seq, const char *name,
707                               const char *units, struct obd_hist_pcpu *read,
708                               struct obd_hist_pcpu *write, bool scale)
709 {
710         unsigned long read_tot, write_tot, r, w, read_cum = 0, write_cum = 0;
711         unsigned int i;
712
713         seq_printf(seq, "\n%26s read      |     write\n", " ");
714         seq_printf(seq, "%-22s %-5s %% cum %% |  %-11s %% cum %%\n",
715                    name, units, units);
716
717         read_tot = lprocfs_oh_sum_pcpu(read);
718         write_tot = lprocfs_oh_sum_pcpu(write);
719
720         if (!read_tot && !write_tot)
721                 return;
722
723         for (i = 0; i < OBD_HIST_MAX; i++) {
724                 r = lprocfs_oh_counter_pcpu(read, i);
725                 w = lprocfs_oh_counter_pcpu(write, i);
726                 read_cum += r;
727                 write_cum += w;
728                 if (read_cum == 0 && write_cum == 0)
729                         continue;
730
731                 if (!scale)
732                         seq_printf(seq, "%u", i);
733                 else if (i < 10)
734                         seq_printf(seq, "%lu", BIT(i));
735                 else if (i < 20)
736                         seq_printf(seq, "%luK", BIT(i - 10));
737                 else
738                         seq_printf(seq, "%luM", BIT(i - 20));
739
740                 seq_printf(seq, ":\t\t%10lu %3u %3u   | %4lu %3u %3u\n",
741                            r, pct(r, read_tot), pct(read_cum, read_tot),
742                            w, pct(w, write_tot), pct(write_cum, write_tot));
743
744                 if (read_cum == read_tot && write_cum == write_tot)
745                         break;
746         }
747 }
748
749 static const struct brw_stats_props brw_props[] = {
750         { .bsp_name     = "pages per bulk r/w",
751           .bsp_units    = "rpcs",
752           .bsp_scale    = true                          },
753         { .bsp_name     = "discontiguous pages",
754           .bsp_units    = "rpcs",
755           .bsp_scale    = false                         },
756         { .bsp_name     = "discontiguous blocks",
757           .bsp_units    = "rpcs",
758           .bsp_scale    = false                         },
759         { .bsp_name     = "disk fragmented I/Os",
760           .bsp_units    = "ios",
761           .bsp_scale    = false                         },
762         { .bsp_name     = "disk I/Os in flight",
763           .bsp_units    = "ios",
764           .bsp_scale    = false                         },
765         { .bsp_name     = "I/O time (1/1000s)",
766           .bsp_units    = "ios",
767           .bsp_scale    = true                          },
768         { .bsp_name     = "disk I/O size",
769           .bsp_units    = "ios",
770           .bsp_scale    = true                          },
771 };
772
773 static int brw_stats_seq_show(struct seq_file *seq, void *v)
774 {
775         struct brw_stats *brw_stats = seq->private;
776         int i;
777
778         /* this sampling races with updates */
779         lprocfs_stats_header(seq, ktime_get(), brw_stats->bs_init, 25, ":", 1);
780
781         for (i = 0; i < ARRAY_SIZE(brw_stats->bs_props); i++) {
782                 if (!brw_stats->bs_props[i].bsp_name)
783                         continue;
784
785                 display_brw_stats(seq, brw_stats->bs_props[i].bsp_name,
786                                   brw_stats->bs_props[i].bsp_units,
787                                   &brw_stats->bs_hist[i * 2],
788                                   &brw_stats->bs_hist[i * 2 + 1],
789                                   brw_stats->bs_props[i].bsp_scale);
790         }
791
792         return 0;
793 }
794
795 static ssize_t brw_stats_seq_write(struct file *file,
796                                    const char __user *buf,
797                                    size_t len, loff_t *off)
798 {
799         struct seq_file *seq = file->private_data;
800         struct brw_stats *brw_stats = seq->private;
801         int i;
802
803         for (i = 0; i < BRW_RW_STATS_NUM; i++)
804                 lprocfs_oh_clear_pcpu(&brw_stats->bs_hist[i]);
805
806         return len;
807 }
808
809 LDEBUGFS_SEQ_FOPS(brw_stats);
810
811 int lprocfs_init_brw_stats(struct brw_stats *brw_stats)
812 {
813         int i, result;
814
815         for (i = 0; i < BRW_RW_STATS_NUM; i++) {
816                 result = lprocfs_oh_alloc_pcpu(&brw_stats->bs_hist[i]);
817                 if (result)
818                         break;
819         }
820
821         return result;
822 }
823 EXPORT_SYMBOL(lprocfs_init_brw_stats);
824
825 void lprocfs_fini_brw_stats(struct brw_stats *brw_stats)
826 {
827         int i;
828
829         for (i = 0; i < BRW_RW_STATS_NUM; i++)
830                 lprocfs_oh_release_pcpu(&brw_stats->bs_hist[i]);
831 }
832 EXPORT_SYMBOL(lprocfs_fini_brw_stats);
833
834 void ldebugfs_register_osd_stats(struct dentry *parent,
835                                  struct brw_stats *brw_stats,
836                                  struct lprocfs_stats *stats)
837 {
838         int i;
839
840         LASSERT(brw_stats);
841         brw_stats->bs_init = ktime_get();
842         for (i = 0; i < BRW_RW_STATS_NUM; i++) {
843                 struct brw_stats_props *props = brw_stats->bs_props;
844
845                 if (i % 2) {
846                         props[i / 2].bsp_name = brw_props[i / 2].bsp_name;
847                         props[i / 2].bsp_units = brw_props[i / 2].bsp_units;
848                         props[i / 2].bsp_scale = brw_props[i / 2].bsp_scale;
849                 }
850         }
851
852         if (!parent)
853                 return;
854
855         debugfs_create_file("brw_stats", 0644, parent, brw_stats,
856                             &brw_stats_fops);
857
858         if (stats)
859                 debugfs_create_file("stats", 0644, parent, stats,
860                                     &ldebugfs_stats_seq_fops);
861 }
862 EXPORT_SYMBOL(ldebugfs_register_osd_stats);
863
864 int lprocfs_hash_seq_show(struct seq_file *m, void *data)
865 {
866         struct obd_device *obd = m->private;
867
868         if (obd == NULL)
869                 return 0;
870
871         /* header for rhashtable state */
872         seq_printf(m, "%-*s   cur   min        max theta t-min t-max flags  rehash   count  maxdep distribution\n",
873                    HASH_NAME_LEN, "name");
874         ldebugfs_rhash_seq_show("UUID_HASH", &obd->obd_uuid_hash, m);
875         ldebugfs_rhash_seq_show("NID_HASH", &obd->obd_nid_hash.ht, m);
876
877         cfs_hash_debug_header(m);
878         cfs_hash_debug_str(obd->obd_nid_stats_hash, m);
879         return 0;
880 }
881 EXPORT_SYMBOL(lprocfs_hash_seq_show);
882
883 int lprocfs_recovery_status_seq_show(struct seq_file *m, void *data)
884 {
885         struct obd_device *obd = m->private;
886         struct target_distribute_txn_data *tdtd;
887
888         LASSERT(obd != NULL);
889
890         seq_printf(m, "status: ");
891         if (atomic_read(&obd->obd_max_recoverable_clients) == 0) {
892                 seq_printf(m, "INACTIVE\n");
893                 goto out;
894         }
895
896         /* There is gap between client data read from storage and setting
897          * obd_recovering so check obd_recovery_end as well to make sure
898          * recovery is really finished
899          */
900         if (obd->obd_recovery_end > 0 && !obd->obd_recovering) {
901                 seq_printf(m, "COMPLETE\n");
902                 seq_printf(m, "recovery_start: %lld\n",
903                            (s64)ktime_get_real_seconds() -
904                            (ktime_get_seconds() - obd->obd_recovery_start));
905                 seq_printf(m, "recovery_duration: %lld\n",
906                            obd->obd_recovery_end ?
907                            obd->obd_recovery_end - obd->obd_recovery_start :
908                            ktime_get_seconds() - obd->obd_recovery_start);
909                 /* Number of clients that have completed recovery */
910                 seq_printf(m, "completed_clients: %d/%d\n",
911                            atomic_read(&obd->obd_max_recoverable_clients) -
912                            obd->obd_stale_clients,
913                            atomic_read(&obd->obd_max_recoverable_clients));
914                 seq_printf(m, "replayed_requests: %d\n",
915                            obd->obd_replayed_requests);
916                 seq_printf(m, "last_transno: %lld\n",
917                            obd->obd_next_recovery_transno - 1);
918                 seq_printf(m, "VBR: %s\n", obd->obd_version_recov ?
919                            "ENABLED" : "DISABLED");
920                 seq_printf(m, "IR: %s\n", obd->obd_no_ir ?
921                            "DISABLED" : "ENABLED");
922                 goto out;
923         }
924
925         tdtd = obd->u.obt.obt_lut->lut_tdtd;
926         if (tdtd && tdtd->tdtd_show_update_logs_retrievers) {
927                 char *buf;
928                 int size = 0;
929                 int count = 0;
930
931                 buf = tdtd->tdtd_show_update_logs_retrievers(
932                         tdtd->tdtd_show_retrievers_cbdata,
933                         &size, &count);
934                 if (count > 0) {
935                         seq_printf(m, "WAITING\n");
936                         seq_printf(m, "non-ready MDTs: %s\n",
937                                    buf ? buf : "unknown (not enough RAM)");
938                         seq_printf(m, "recovery_start: %lld\n",
939                                    (s64)ktime_get_real_seconds() -
940                                    (ktime_get_seconds() -
941                                     obd->obd_recovery_start));
942                         seq_printf(m, "time_waited: %lld\n",
943                                    (s64)(ktime_get_seconds() -
944                                          obd->obd_recovery_start));
945                 }
946
947                 if (buf != NULL)
948                         OBD_FREE(buf, size);
949
950                 if (likely(count > 0))
951                         goto out;
952         }
953
954         /* recovery won't start until the clients connect */
955         if (obd->obd_recovery_start == 0) {
956                 seq_printf(m, "WAITING_FOR_CLIENTS\n");
957                 goto out;
958         }
959
960         seq_printf(m, "RECOVERING\n");
961         seq_printf(m, "recovery_start: %lld\n", (s64)ktime_get_real_seconds() -
962                    (ktime_get_seconds() - obd->obd_recovery_start));
963         seq_printf(m, "time_remaining: %lld\n",
964                    ktime_get_seconds() >=
965                    obd->obd_recovery_start +
966                    obd->obd_recovery_timeout ? 0 :
967                    (s64)(obd->obd_recovery_start +
968                          obd->obd_recovery_timeout -
969                          ktime_get_seconds()));
970         seq_printf(m, "connected_clients: %d/%d\n",
971                    atomic_read(&obd->obd_connected_clients),
972                    atomic_read(&obd->obd_max_recoverable_clients));
973         /* Number of clients that have completed recovery */
974         seq_printf(m, "req_replay_clients: %d\n",
975                    atomic_read(&obd->obd_req_replay_clients));
976         seq_printf(m, "lock_repay_clients: %d\n",
977                    atomic_read(&obd->obd_lock_replay_clients));
978         seq_printf(m, "completed_clients: %d\n",
979                    atomic_read(&obd->obd_connected_clients) -
980                    atomic_read(&obd->obd_lock_replay_clients));
981         seq_printf(m, "evicted_clients: %d\n", obd->obd_stale_clients);
982         seq_printf(m, "replayed_requests: %d\n", obd->obd_replayed_requests);
983         seq_printf(m, "queued_requests: %d\n",
984                    obd->obd_requests_queued_for_recovery);
985         seq_printf(m, "next_transno: %lld\n",
986                    obd->obd_next_recovery_transno);
987 out:
988         return 0;
989 }
990 EXPORT_SYMBOL(lprocfs_recovery_status_seq_show);
991
992 ssize_t ir_factor_show(struct kobject *kobj, struct attribute *attr,
993                        char *buf)
994 {
995         struct obd_device *obd = container_of(kobj, struct obd_device,
996                                               obd_kset.kobj);
997
998         return scnprintf(buf, PAGE_SIZE, "%d\n", obd->obd_recovery_ir_factor);
999 }
1000 EXPORT_SYMBOL(ir_factor_show);
1001
1002 ssize_t ir_factor_store(struct kobject *kobj, struct attribute *attr,
1003                         const char *buffer, size_t count)
1004 {
1005         struct obd_device *obd = container_of(kobj, struct obd_device,
1006                                               obd_kset.kobj);
1007         int val;
1008         int rc;
1009
1010         rc = kstrtoint(buffer, 10, &val);
1011         if (rc)
1012                 return rc;
1013
1014         if (val < OBD_IR_FACTOR_MIN || val > OBD_IR_FACTOR_MAX)
1015                 return -EINVAL;
1016
1017         obd->obd_recovery_ir_factor = val;
1018         return count;
1019 }
1020 EXPORT_SYMBOL(ir_factor_store);
1021
1022 int lprocfs_checksum_dump_seq_show(struct seq_file *m, void *data)
1023 {
1024         struct obd_device *obd = m->private;
1025
1026         LASSERT(obd != NULL);
1027         seq_printf(m, "%d\n", obd->obd_checksum_dump);
1028         return 0;
1029 }
1030 EXPORT_SYMBOL(lprocfs_checksum_dump_seq_show);
1031
1032 ssize_t
1033 lprocfs_checksum_dump_seq_write(struct file *file, const char __user *buffer,
1034                             size_t count, loff_t *off)
1035 {
1036         struct seq_file *m = file->private_data;
1037         struct obd_device *obd = m->private;
1038         bool val;
1039         int rc;
1040
1041         LASSERT(obd != NULL);
1042         rc = kstrtobool_from_user(buffer, count, &val);
1043         if (rc)
1044                 return rc;
1045
1046         obd->obd_checksum_dump = val;
1047         return count;
1048 }
1049 EXPORT_SYMBOL(lprocfs_checksum_dump_seq_write);
1050
1051 ssize_t recovery_time_soft_show(struct kobject *kobj, struct attribute *attr,
1052                                 char *buf)
1053 {
1054         struct obd_device *obd = container_of(kobj, struct obd_device,
1055                                               obd_kset.kobj);
1056
1057         return scnprintf(buf, PAGE_SIZE, "%d\n", obd->obd_recovery_timeout);
1058 }
1059 EXPORT_SYMBOL(recovery_time_soft_show);
1060
1061 ssize_t recovery_time_soft_store(struct kobject *kobj,
1062                                  struct attribute *attr,
1063                                  const char *buffer, size_t count)
1064 {
1065         struct obd_device *obd = container_of(kobj, struct obd_device,
1066                                               obd_kset.kobj);
1067         unsigned int val;
1068         int rc;
1069
1070         rc = kstrtouint(buffer, 0, &val);
1071         if (rc)
1072                 return rc;
1073
1074         obd->obd_recovery_timeout = val;
1075         return count;
1076 }
1077 EXPORT_SYMBOL(recovery_time_soft_store);
1078
1079 ssize_t recovery_time_hard_show(struct kobject *kobj, struct attribute *attr,
1080                                 char *buf)
1081 {
1082         struct obd_device *obd = container_of(kobj, struct obd_device,
1083                                               obd_kset.kobj);
1084
1085         return scnprintf(buf, PAGE_SIZE, "%d\n", obd->obd_recovery_time_hard);
1086 }
1087 EXPORT_SYMBOL(recovery_time_hard_show);
1088
1089 ssize_t recovery_time_hard_store(struct kobject *kobj,
1090                                  struct attribute *attr,
1091                                  const char *buffer, size_t count)
1092 {
1093         struct obd_device *obd = container_of(kobj, struct obd_device,
1094                                               obd_kset.kobj);
1095         unsigned int val;
1096         int rc;
1097
1098         rc = kstrtouint(buffer, 0, &val);
1099         if (rc)
1100                 return rc;
1101
1102         obd->obd_recovery_time_hard = val;
1103         return count;
1104 }
1105 EXPORT_SYMBOL(recovery_time_hard_store);
1106
1107 ssize_t instance_show(struct kobject *kobj, struct attribute *attr,
1108                       char *buf)
1109 {
1110         struct obd_device *obd = container_of(kobj, struct obd_device,
1111                                               obd_kset.kobj);
1112         struct obd_device_target *target = &obd->u.obt;
1113
1114         LASSERT(target->obt_magic == OBT_MAGIC);
1115         return scnprintf(buf, PAGE_SIZE, "%u\n", obd->u.obt.obt_instance);
1116 }
1117 EXPORT_SYMBOL(instance_show);
1118
1119 #endif /* CONFIG_PROC_FS*/