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