Whamcloud - gitweb
9a30639ceabbd8f0b330888652a5483516629aa4
[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_nid2str(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         new_stat->nid     = *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                                            nid, &new_stat->nid_hash);
557         CDEBUG(D_INFO, "Found stats %p for nid %s - ref %d\n",
558                old_stat, nidstr, atomic_read(&old_stat->nid_exp_ref_count));
559
560         /* Return -EALREADY here so that we know that the /proc
561          * entry already has been created */
562         if (old_stat != new_stat) {
563                 spin_lock(&exp->exp_lock);
564                 if (exp->exp_nid_stats) {
565                         LASSERT(exp->exp_nid_stats == old_stat);
566                         nidstat_putref(exp->exp_nid_stats);
567                 }
568                 exp->exp_nid_stats = old_stat;
569                 spin_unlock(&exp->exp_lock);
570                 GOTO(destroy_new, rc = -EALREADY);
571         }
572         /* not found - create */
573         new_stat->nid_proc = lprocfs_register(nidstr,
574                                               obd->obd_proc_exports_entry,
575                                               NULL, NULL);
576
577         if (IS_ERR(new_stat->nid_proc)) {
578                 rc = PTR_ERR(new_stat->nid_proc);
579                 new_stat->nid_proc = NULL;
580                 CERROR("%s: cannot create proc entry for export %s: rc = %d\n",
581                        obd->obd_name, nidstr, rc);
582                 GOTO(destroy_new_ns, rc);
583         }
584
585         entry = lprocfs_add_simple(new_stat->nid_proc, "nodemap", new_stat,
586                                    &lprocfs_exp_nodemap_fops);
587         if (IS_ERR(entry)) {
588                 rc = PTR_ERR(entry);
589                 CWARN("%s: error adding the nodemap file: rc = %d\n",
590                       obd->obd_name, rc);
591                 GOTO(destroy_new_ns, rc);
592         }
593
594         entry = lprocfs_add_simple(new_stat->nid_proc, "uuid", new_stat,
595                                    &lprocfs_exp_uuid_fops);
596         if (IS_ERR(entry)) {
597                 rc = PTR_ERR(entry);
598                 CWARN("%s: error adding the NID stats file: rc = %d\n",
599                       obd->obd_name, rc);
600                 GOTO(destroy_new_ns, rc);
601         }
602
603         entry = lprocfs_add_simple(new_stat->nid_proc, "hash", new_stat,
604                                    &lprocfs_exp_hash_fops);
605         if (IS_ERR(entry)) {
606                 rc = PTR_ERR(entry);
607                 CWARN("%s: error adding the hash file: rc = %d\n",
608                       obd->obd_name, rc);
609                 GOTO(destroy_new_ns, rc);
610         }
611
612         entry = lprocfs_add_simple(new_stat->nid_proc, "export",
613                                    new_stat, &lprocfs_exp_export_fops);
614         if (IS_ERR(entry)) {
615                 rc = PTR_ERR(entry);
616                 CWARN("%s: error adding the export file: rc = %d\n",
617                       obd->obd_name, rc);
618                 GOTO(destroy_new_ns, rc);
619         }
620
621         entry = lprocfs_add_simple(new_stat->nid_proc, "reply_data", new_stat,
622                                    &lprocfs_exp_replydata_fops);
623         if (IS_ERR(entry)) {
624                 rc = PTR_ERR(entry);
625                 CWARN("%s: error adding the reply_data file: rc = %d\n",
626                       obd->obd_name, rc);
627                 GOTO(destroy_new_ns, rc);
628         }
629
630         entry = lprocfs_add_simple(new_stat->nid_proc, "fmd_count", new_stat,
631                                    &lprocfs_exp_fmd_count_fops);
632         if (IS_ERR(entry)) {
633                 rc = PTR_ERR(entry);
634                 CWARN("%s: error adding the fmd_count file: rc = %d\n",
635                       obd->obd_name, rc);
636                 GOTO(destroy_new_ns, rc);
637         }
638
639         spin_lock(&exp->exp_lock);
640         exp->exp_nid_stats = new_stat;
641         spin_unlock(&exp->exp_lock);
642
643         /* protect competitive add to list, not need locking on destroy */
644         spin_lock(&obd->obd_nid_lock);
645         list_add(&new_stat->nid_list, &obd->obd_nid_stats);
646         spin_unlock(&obd->obd_nid_lock);
647
648         RETURN(0);
649
650 destroy_new_ns:
651         if (new_stat->nid_proc != NULL)
652                 lprocfs_remove(&new_stat->nid_proc);
653         cfs_hash_del(obd->obd_nid_stats_hash, nid, &new_stat->nid_hash);
654
655 destroy_new:
656         nidstat_putref(new_stat);
657         OBD_FREE_PTR(new_stat);
658         RETURN(rc);
659 }
660 EXPORT_SYMBOL(lprocfs_exp_setup);
661
662 int lprocfs_exp_cleanup(struct obd_export *exp)
663 {
664         struct nid_stat *stat = exp->exp_nid_stats;
665
666         if (!stat || !exp->exp_obd)
667                 RETURN(0);
668
669         nidstat_putref(exp->exp_nid_stats);
670         exp->exp_nid_stats = NULL;
671
672         return 0;
673 }
674
675 int lprocfs_alloc_obd_stats(struct obd_device *obd, unsigned int num_stats)
676 {
677         struct lprocfs_stats *stats;
678         int rc;
679
680         LASSERT(obd->obd_stats == NULL);
681         LASSERT(obd->obd_proc_entry != NULL);
682
683         stats = lprocfs_alloc_stats(num_stats, 0);
684         if (stats == NULL)
685                 return -ENOMEM;
686
687         rc = lprocfs_register_stats(obd->obd_proc_entry, "stats", stats);
688         if (rc < 0)
689                 lprocfs_free_stats(&stats);
690         else
691                 obd->obd_stats = stats;
692
693         return rc;
694 }
695 EXPORT_SYMBOL(lprocfs_alloc_obd_stats);
696
697 void lprocfs_free_obd_stats(struct obd_device *obd)
698 {
699         if (obd->obd_stats)
700                 lprocfs_free_stats(&obd->obd_stats);
701 }
702 EXPORT_SYMBOL(lprocfs_free_obd_stats);
703
704 static void display_brw_stats(struct seq_file *seq, const char *name,
705                               const char *units, struct obd_hist_pcpu *read,
706                               struct obd_hist_pcpu *write, bool scale)
707 {
708         unsigned long read_tot, write_tot, r, w, read_cum = 0, write_cum = 0;
709         unsigned int i;
710
711         seq_printf(seq, "\n%26s read      |     write\n", " ");
712         seq_printf(seq, "%-22s %-5s %% cum %% |  %-11s %% cum %%\n",
713                    name, units, units);
714
715         read_tot = lprocfs_oh_sum_pcpu(read);
716         write_tot = lprocfs_oh_sum_pcpu(write);
717
718         if (!read_tot && !write_tot)
719                 return;
720
721         for (i = 0; i < OBD_HIST_MAX; i++) {
722                 r = lprocfs_oh_counter_pcpu(read, i);
723                 w = lprocfs_oh_counter_pcpu(write, i);
724                 read_cum += r;
725                 write_cum += w;
726                 if (read_cum == 0 && write_cum == 0)
727                         continue;
728
729                 if (!scale)
730                         seq_printf(seq, "%u", i);
731                 else if (i < 10)
732                         seq_printf(seq, "%lu", BIT(i));
733                 else if (i < 20)
734                         seq_printf(seq, "%luK", BIT(i - 10));
735                 else
736                         seq_printf(seq, "%luM", BIT(i - 20));
737
738                 seq_printf(seq, ":\t\t%10lu %3u %3u   | %4lu %3u %3u\n",
739                            r, pct(r, read_tot), pct(read_cum, read_tot),
740                            w, pct(w, write_tot), pct(write_cum, write_tot));
741
742                 if (read_cum == read_tot && write_cum == write_tot)
743                         break;
744         }
745 }
746
747 static const struct brw_stats_props brw_props[] = {
748         { .bsp_name     = "pages per bulk r/w",
749           .bsp_units    = "rpcs",
750           .bsp_scale    = true                          },
751         { .bsp_name     = "discontiguous pages",
752           .bsp_units    = "rpcs",
753           .bsp_scale    = false                         },
754         { .bsp_name     = "discontiguous blocks",
755           .bsp_units    = "rpcs",
756           .bsp_scale    = false                         },
757         { .bsp_name     = "disk fragmented I/Os",
758           .bsp_units    = "ios",
759           .bsp_scale    = false                         },
760         { .bsp_name     = "disk I/Os in flight",
761           .bsp_units    = "ios",
762           .bsp_scale    = false                         },
763         { .bsp_name     = "I/O time (1/1000s)",
764           .bsp_units    = "ios",
765           .bsp_scale    = true                          },
766         { .bsp_name     = "disk I/O size",
767           .bsp_units    = "ios",
768           .bsp_scale    = true                          },
769 };
770
771 static int brw_stats_seq_show(struct seq_file *seq, void *v)
772 {
773         struct brw_stats *brw_stats = seq->private;
774         int i;
775
776         /* this sampling races with updates */
777         lprocfs_stats_header(seq, ktime_get(), brw_stats->bs_init, 25, ":", 1);
778
779         for (i = 0; i < ARRAY_SIZE(brw_stats->bs_props); i++) {
780                 if (!brw_stats->bs_props[i].bsp_name)
781                         continue;
782
783                 display_brw_stats(seq, brw_stats->bs_props[i].bsp_name,
784                                   brw_stats->bs_props[i].bsp_units,
785                                   &brw_stats->bs_hist[i * 2],
786                                   &brw_stats->bs_hist[i * 2 + 1],
787                                   brw_stats->bs_props[i].bsp_scale);
788         }
789
790         return 0;
791 }
792
793 static ssize_t brw_stats_seq_write(struct file *file,
794                                    const char __user *buf,
795                                    size_t len, loff_t *off)
796 {
797         struct seq_file *seq = file->private_data;
798         struct brw_stats *brw_stats = seq->private;
799         int i;
800
801         for (i = 0; i < BRW_RW_STATS_NUM; i++)
802                 lprocfs_oh_clear_pcpu(&brw_stats->bs_hist[i]);
803
804         return len;
805 }
806
807 LDEBUGFS_SEQ_FOPS(brw_stats);
808
809 int lprocfs_init_brw_stats(struct brw_stats *brw_stats)
810 {
811         int i, result;
812
813         for (i = 0; i < BRW_RW_STATS_NUM; i++) {
814                 result = lprocfs_oh_alloc_pcpu(&brw_stats->bs_hist[i]);
815                 if (result)
816                         break;
817         }
818
819         return result;
820 }
821 EXPORT_SYMBOL(lprocfs_init_brw_stats);
822
823 void lprocfs_fini_brw_stats(struct brw_stats *brw_stats)
824 {
825         int i;
826
827         for (i = 0; i < BRW_RW_STATS_NUM; i++)
828                 lprocfs_oh_release_pcpu(&brw_stats->bs_hist[i]);
829 }
830 EXPORT_SYMBOL(lprocfs_fini_brw_stats);
831
832 void ldebugfs_register_osd_stats(struct dentry *parent,
833                                  struct brw_stats *brw_stats,
834                                  struct lprocfs_stats *stats)
835 {
836         int i;
837
838         LASSERT(brw_stats);
839         brw_stats->bs_init = ktime_get();
840         for (i = 0; i < BRW_RW_STATS_NUM; i++) {
841                 struct brw_stats_props *props = brw_stats->bs_props;
842
843                 if (i % 2) {
844                         props[i / 2].bsp_name = brw_props[i / 2].bsp_name;
845                         props[i / 2].bsp_units = brw_props[i / 2].bsp_units;
846                         props[i / 2].bsp_scale = brw_props[i / 2].bsp_scale;
847                 }
848         }
849
850         if (!parent)
851                 return;
852
853         debugfs_create_file("brw_stats", 0644, parent, brw_stats,
854                             &brw_stats_fops);
855
856         if (stats)
857                 debugfs_create_file("stats", 0644, parent, stats,
858                                     &ldebugfs_stats_seq_fops);
859 }
860 EXPORT_SYMBOL(ldebugfs_register_osd_stats);
861
862 int lprocfs_hash_seq_show(struct seq_file *m, void *data)
863 {
864         struct obd_device *obd = m->private;
865
866         if (obd == NULL)
867                 return 0;
868
869         /* header for rhashtable state */
870         seq_printf(m, "%-*s   cur   min        max theta t-min t-max flags  rehash   count  maxdep distribution\n",
871                    HASH_NAME_LEN, "name");
872         ldebugfs_rhash_seq_show("UUID_HASH", &obd->obd_uuid_hash, m);
873         ldebugfs_rhash_seq_show("NID_HASH", &obd->obd_nid_hash.ht, m);
874
875         cfs_hash_debug_header(m);
876         cfs_hash_debug_str(obd->obd_nid_stats_hash, m);
877         return 0;
878 }
879 EXPORT_SYMBOL(lprocfs_hash_seq_show);
880
881 int lprocfs_recovery_status_seq_show(struct seq_file *m, void *data)
882 {
883         struct obd_device *obd = m->private;
884         struct target_distribute_txn_data *tdtd;
885
886         LASSERT(obd != NULL);
887
888         seq_printf(m, "status: ");
889         if (atomic_read(&obd->obd_max_recoverable_clients) == 0) {
890                 seq_printf(m, "INACTIVE\n");
891                 goto out;
892         }
893
894         /* There is gap between client data read from storage and setting
895          * obd_recovering so check obd_recovery_end as well to make sure
896          * recovery is really finished
897          */
898         if (obd->obd_recovery_end > 0 && !obd->obd_recovering) {
899                 seq_printf(m, "COMPLETE\n");
900                 seq_printf(m, "recovery_start: %lld\n",
901                            (s64)ktime_get_real_seconds() -
902                            (ktime_get_seconds() - obd->obd_recovery_start));
903                 seq_printf(m, "recovery_duration: %lld\n",
904                            obd->obd_recovery_end ?
905                            obd->obd_recovery_end - obd->obd_recovery_start :
906                            ktime_get_seconds() - obd->obd_recovery_start);
907                 /* Number of clients that have completed recovery */
908                 seq_printf(m, "completed_clients: %d/%d\n",
909                            atomic_read(&obd->obd_max_recoverable_clients) -
910                            obd->obd_stale_clients,
911                            atomic_read(&obd->obd_max_recoverable_clients));
912                 seq_printf(m, "replayed_requests: %d\n",
913                            obd->obd_replayed_requests);
914                 seq_printf(m, "last_transno: %lld\n",
915                            obd->obd_next_recovery_transno - 1);
916                 seq_printf(m, "VBR: %s\n", obd->obd_version_recov ?
917                            "ENABLED" : "DISABLED");
918                 seq_printf(m, "IR: %s\n", obd->obd_no_ir ?
919                            "DISABLED" : "ENABLED");
920                 goto out;
921         }
922
923         tdtd = obd->u.obt.obt_lut->lut_tdtd;
924         if (tdtd && tdtd->tdtd_show_update_logs_retrievers) {
925                 char *buf;
926                 int size = 0;
927                 int count = 0;
928
929                 buf = tdtd->tdtd_show_update_logs_retrievers(
930                         tdtd->tdtd_show_retrievers_cbdata,
931                         &size, &count);
932                 if (count > 0) {
933                         seq_printf(m, "WAITING\n");
934                         seq_printf(m, "non-ready MDTs: %s\n",
935                                    buf ? buf : "unknown (not enough RAM)");
936                         seq_printf(m, "recovery_start: %lld\n",
937                                    (s64)ktime_get_real_seconds() -
938                                    (ktime_get_seconds() -
939                                     obd->obd_recovery_start));
940                         seq_printf(m, "time_waited: %lld\n",
941                                    (s64)(ktime_get_seconds() -
942                                          obd->obd_recovery_start));
943                 }
944
945                 if (buf != NULL)
946                         OBD_FREE(buf, size);
947
948                 if (likely(count > 0))
949                         goto out;
950         }
951
952         /* recovery won't start until the clients connect */
953         if (obd->obd_recovery_start == 0) {
954                 seq_printf(m, "WAITING_FOR_CLIENTS\n");
955                 goto out;
956         }
957
958         seq_printf(m, "RECOVERING\n");
959         seq_printf(m, "recovery_start: %lld\n", (s64)ktime_get_real_seconds() -
960                    (ktime_get_seconds() - obd->obd_recovery_start));
961         seq_printf(m, "time_remaining: %lld\n",
962                    ktime_get_seconds() >=
963                    obd->obd_recovery_start +
964                    obd->obd_recovery_timeout ? 0 :
965                    (s64)(obd->obd_recovery_start +
966                          obd->obd_recovery_timeout -
967                          ktime_get_seconds()));
968         seq_printf(m, "connected_clients: %d/%d\n",
969                    atomic_read(&obd->obd_connected_clients),
970                    atomic_read(&obd->obd_max_recoverable_clients));
971         /* Number of clients that have completed recovery */
972         seq_printf(m, "req_replay_clients: %d\n",
973                    atomic_read(&obd->obd_req_replay_clients));
974         seq_printf(m, "lock_repay_clients: %d\n",
975                    atomic_read(&obd->obd_lock_replay_clients));
976         seq_printf(m, "completed_clients: %d\n",
977                    atomic_read(&obd->obd_connected_clients) -
978                    atomic_read(&obd->obd_lock_replay_clients));
979         seq_printf(m, "evicted_clients: %d\n", obd->obd_stale_clients);
980         seq_printf(m, "replayed_requests: %d\n", obd->obd_replayed_requests);
981         seq_printf(m, "queued_requests: %d\n",
982                    obd->obd_requests_queued_for_recovery);
983         seq_printf(m, "next_transno: %lld\n",
984                    obd->obd_next_recovery_transno);
985 out:
986         return 0;
987 }
988 EXPORT_SYMBOL(lprocfs_recovery_status_seq_show);
989
990 ssize_t ir_factor_show(struct kobject *kobj, struct attribute *attr,
991                        char *buf)
992 {
993         struct obd_device *obd = container_of(kobj, struct obd_device,
994                                               obd_kset.kobj);
995
996         return scnprintf(buf, PAGE_SIZE, "%d\n", obd->obd_recovery_ir_factor);
997 }
998 EXPORT_SYMBOL(ir_factor_show);
999
1000 ssize_t ir_factor_store(struct kobject *kobj, struct attribute *attr,
1001                         const char *buffer, size_t count)
1002 {
1003         struct obd_device *obd = container_of(kobj, struct obd_device,
1004                                               obd_kset.kobj);
1005         int val;
1006         int rc;
1007
1008         rc = kstrtoint(buffer, 10, &val);
1009         if (rc)
1010                 return rc;
1011
1012         if (val < OBD_IR_FACTOR_MIN || val > OBD_IR_FACTOR_MAX)
1013                 return -EINVAL;
1014
1015         obd->obd_recovery_ir_factor = val;
1016         return count;
1017 }
1018 EXPORT_SYMBOL(ir_factor_store);
1019
1020 int lprocfs_checksum_dump_seq_show(struct seq_file *m, void *data)
1021 {
1022         struct obd_device *obd = m->private;
1023
1024         LASSERT(obd != NULL);
1025         seq_printf(m, "%d\n", obd->obd_checksum_dump);
1026         return 0;
1027 }
1028 EXPORT_SYMBOL(lprocfs_checksum_dump_seq_show);
1029
1030 ssize_t
1031 lprocfs_checksum_dump_seq_write(struct file *file, const char __user *buffer,
1032                             size_t count, loff_t *off)
1033 {
1034         struct seq_file *m = file->private_data;
1035         struct obd_device *obd = m->private;
1036         bool val;
1037         int rc;
1038
1039         LASSERT(obd != NULL);
1040         rc = kstrtobool_from_user(buffer, count, &val);
1041         if (rc)
1042                 return rc;
1043
1044         obd->obd_checksum_dump = val;
1045         return count;
1046 }
1047 EXPORT_SYMBOL(lprocfs_checksum_dump_seq_write);
1048
1049 ssize_t recovery_time_soft_show(struct kobject *kobj, struct attribute *attr,
1050                                 char *buf)
1051 {
1052         struct obd_device *obd = container_of(kobj, struct obd_device,
1053                                               obd_kset.kobj);
1054
1055         return scnprintf(buf, PAGE_SIZE, "%d\n", obd->obd_recovery_timeout);
1056 }
1057 EXPORT_SYMBOL(recovery_time_soft_show);
1058
1059 ssize_t recovery_time_soft_store(struct kobject *kobj,
1060                                  struct attribute *attr,
1061                                  const char *buffer, size_t count)
1062 {
1063         struct obd_device *obd = container_of(kobj, struct obd_device,
1064                                               obd_kset.kobj);
1065         unsigned int val;
1066         int rc;
1067
1068         rc = kstrtouint(buffer, 0, &val);
1069         if (rc)
1070                 return rc;
1071
1072         obd->obd_recovery_timeout = val;
1073         return count;
1074 }
1075 EXPORT_SYMBOL(recovery_time_soft_store);
1076
1077 ssize_t recovery_time_hard_show(struct kobject *kobj, struct attribute *attr,
1078                                 char *buf)
1079 {
1080         struct obd_device *obd = container_of(kobj, struct obd_device,
1081                                               obd_kset.kobj);
1082
1083         return scnprintf(buf, PAGE_SIZE, "%d\n", obd->obd_recovery_time_hard);
1084 }
1085 EXPORT_SYMBOL(recovery_time_hard_show);
1086
1087 ssize_t recovery_time_hard_store(struct kobject *kobj,
1088                                  struct attribute *attr,
1089                                  const char *buffer, size_t count)
1090 {
1091         struct obd_device *obd = container_of(kobj, struct obd_device,
1092                                               obd_kset.kobj);
1093         unsigned int val;
1094         int rc;
1095
1096         rc = kstrtouint(buffer, 0, &val);
1097         if (rc)
1098                 return rc;
1099
1100         obd->obd_recovery_time_hard = val;
1101         return count;
1102 }
1103 EXPORT_SYMBOL(recovery_time_hard_store);
1104
1105 ssize_t instance_show(struct kobject *kobj, struct attribute *attr,
1106                       char *buf)
1107 {
1108         struct obd_device *obd = container_of(kobj, struct obd_device,
1109                                               obd_kset.kobj);
1110         struct obd_device_target *target = &obd->u.obt;
1111
1112         LASSERT(target->obt_magic == OBT_MAGIC);
1113         return scnprintf(buf, PAGE_SIZE, "%u\n", obd->u.obt.obt_instance);
1114 }
1115 EXPORT_SYMBOL(instance_show);
1116
1117 #endif /* CONFIG_PROC_FS*/