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