Whamcloud - gitweb
LU-8066 ptlrpc: migrate ptlrpc proc files to sysfs
[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, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/lprocfs_status_server.c
33  */
34
35 #define DEBUG_SUBSYSTEM S_CLASS
36
37 #include <linux/kobject.h>
38 #include <linux/sysfs.h>
39
40 #include <obd_class.h>
41 #include <lprocfs_status.h>
42 #include <lustre/lustre_idl.h>
43 #include <lustre_nodemap.h>
44
45 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
46                          char *buf)
47 {
48         struct obd_device *obd = class_name2obd(kobj->name);
49
50         return sprintf(buf, "%s\n", obd->obd_uuid.uuid);
51 }
52 LUSTRE_RO_ATTR(uuid);
53
54 static struct attribute *server_uuid_attrs[] = {
55         &lustre_attr_uuid.attr,
56         NULL,
57 };
58
59 static struct attribute_group server_def_attr_group = {
60         .attrs = server_uuid_attrs,
61 };
62
63 void lprocfs_kset_unregister(struct obd_device *obd, struct kset *kset)
64 {
65         if (obd->obd_proc_entry) {
66                 lprocfs_remove(&obd->obd_proc_entry);
67                 obd->obd_proc_entry = NULL;
68         }
69
70         if (kset)
71                 kset_unregister(kset);
72 }
73 EXPORT_SYMBOL(lprocfs_kset_unregister);
74
75 int lprocfs_kset_register(struct obd_device *obd, struct kset **kset)
76 {
77         struct kset *obd_kset;
78         int rc = 0;
79
80         obd_kset = kset_create_and_add(obd->obd_name, NULL,
81                                        obd->obd_type->typ_kobj);
82         if (!obd_kset) {
83                 CERROR("%s: cannot add new kset: rc = %d\n", obd->obd_name, rc);
84                 rc = -ENOMEM;
85         }
86
87         if (!obd->obd_attrs)
88                 obd->obd_attrs = &server_def_attr_group;
89
90         rc = sysfs_create_group(&obd_kset->kobj, obd->obd_attrs);
91         if (rc) {
92                 CERROR("%s: cannot create attr group: rc = %d\n", obd->obd_name,
93                        rc);
94                 kset_unregister(obd_kset);
95                 obd_kset = NULL;
96                 return rc;
97         }
98
99         obd->obd_proc_entry = lprocfs_register(obd->obd_name,
100                                                obd->obd_type->typ_procroot,
101                                                obd->obd_vars, obd);
102         if (IS_ERR(obd->obd_proc_entry)) {
103                 rc = PTR_ERR(obd->obd_proc_entry);
104                 CERROR("%s: error %d setting up lprocfs\n", obd->obd_name, rc);
105                 obd->obd_proc_entry = NULL;
106
107                 kset_unregister(obd_kset);
108                 obd_kset = NULL;
109         }
110         *kset = obd_kset;
111         return rc;
112 }
113 EXPORT_SYMBOL(lprocfs_kset_register);
114
115 #ifdef CONFIG_PROC_FS
116
117 int lprocfs_evict_client_open(struct inode *inode, struct file *f)
118 {
119         struct obd_device *obd = PDE_DATA(file_inode(f));
120
121         atomic_inc(&obd->obd_evict_inprogress);
122         return 0;
123 }
124
125 int lprocfs_evict_client_release(struct inode *inode, struct file *f)
126 {
127         struct obd_device *obd = PDE_DATA(file_inode(f));
128
129         atomic_dec(&obd->obd_evict_inprogress);
130         wake_up(&obd->obd_evict_inprogress_waitq);
131
132         return 0;
133 }
134
135 #define BUFLEN (UUID_MAX + 5)
136
137 ssize_t
138 lprocfs_evict_client_seq_write(struct file *file, const char __user *buffer,
139                                size_t count, loff_t *off)
140 {
141         struct seq_file *m = file->private_data;
142         struct obd_device *obd = m->private;
143         char *tmpbuf, *kbuf;
144
145         OBD_ALLOC(kbuf, BUFLEN);
146         if (kbuf == NULL)
147                 return -ENOMEM;
148
149         /*
150          * OBD_ALLOC() will zero kbuf, but we only copy BUFLEN - 1
151          * bytes into kbuf, to ensure that the string is NUL-terminated.
152          * UUID_MAX should include a trailing NUL already.
153          */
154         if (copy_from_user(kbuf, buffer,
155                            min_t(unsigned long, BUFLEN - 1, count))) {
156                 count = -EFAULT;
157                 goto out;
158         }
159         tmpbuf = cfs_firststr(kbuf, min_t(unsigned long, BUFLEN - 1, count));
160         class_incref(obd, __func__, current);
161
162         if (strncmp(tmpbuf, "nid:", 4) == 0)
163                 obd_export_evict_by_nid(obd, tmpbuf + 4);
164         else if (strncmp(tmpbuf, "uuid:", 5) == 0)
165                 obd_export_evict_by_uuid(obd, tmpbuf + 5);
166         else
167                 obd_export_evict_by_uuid(obd, tmpbuf);
168
169         class_decref(obd, __func__, current);
170
171 out:
172         OBD_FREE(kbuf, BUFLEN);
173         return count;
174 }
175 EXPORT_SYMBOL(lprocfs_evict_client_seq_write);
176
177 #undef BUFLEN
178
179 int lprocfs_num_exports_seq_show(struct seq_file *m, void *data)
180 {
181         struct obd_device *obd = data;
182
183         LASSERT(obd != NULL);
184         seq_printf(m, "%u\n", obd->obd_num_exports);
185         return 0;
186 }
187 EXPORT_SYMBOL(lprocfs_num_exports_seq_show);
188
189 static void lprocfs_free_client_stats(struct nid_stat *client_stat)
190 {
191         CDEBUG(D_CONFIG, "stat %p - data %p/%p\n", client_stat,
192                client_stat->nid_proc, client_stat->nid_stats);
193
194         LASSERTF(atomic_read(&client_stat->nid_exp_ref_count) == 0,
195                  "nid %s:count %d\n", libcfs_nid2str(client_stat->nid),
196                  atomic_read(&client_stat->nid_exp_ref_count));
197
198         if (client_stat->nid_proc)
199                 lprocfs_remove(&client_stat->nid_proc);
200
201         if (client_stat->nid_stats)
202                 lprocfs_free_stats(&client_stat->nid_stats);
203
204         if (client_stat->nid_ldlm_stats)
205                 lprocfs_free_stats(&client_stat->nid_ldlm_stats);
206
207         OBD_FREE_PTR(client_stat);
208         return;
209 }
210
211 void lprocfs_free_per_client_stats(struct obd_device *obd)
212 {
213         struct cfs_hash *hash = obd->obd_nid_stats_hash;
214         struct nid_stat *stat;
215         ENTRY;
216
217         /* we need extra list - because hash_exit called to early */
218         /* not need locking because all clients is died */
219         while (!list_empty(&obd->obd_nid_stats)) {
220                 stat = list_entry(obd->obd_nid_stats.next,
221                                   struct nid_stat, nid_list);
222                 list_del_init(&stat->nid_list);
223                 cfs_hash_del(hash, &stat->nid, &stat->nid_hash);
224                 lprocfs_free_client_stats(stat);
225         }
226         EXIT;
227 }
228 EXPORT_SYMBOL(lprocfs_free_per_client_stats);
229
230 static int
231 lprocfs_exp_print_uuid_seq(struct cfs_hash *hs, struct cfs_hash_bd *bd,
232                            struct hlist_node *hnode, void *cb_data)
233 {
234         struct seq_file *m = cb_data;
235         struct obd_export *exp = cfs_hash_object(hs, hnode);
236
237         if (exp->exp_nid_stats != NULL)
238                 seq_printf(m, "%s\n", obd_uuid2str(&exp->exp_client_uuid));
239         return 0;
240 }
241
242 static int
243 lprocfs_exp_print_nodemap_seq(struct cfs_hash *hs, struct cfs_hash_bd *bd,
244                               struct hlist_node *hnode, void *cb_data)
245 {
246         struct seq_file *m = cb_data;
247         struct obd_export *exp = cfs_hash_object(hs, hnode);
248         struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
249
250         if (nodemap != NULL)
251                 seq_printf(m, "%s\n", nodemap->nm_name);
252         return 0;
253 }
254
255 static int
256 lprocfs_exp_nodemap_seq_show(struct seq_file *m, void *data)
257 {
258         struct nid_stat *stats = m->private;
259         struct obd_device *obd = stats->nid_obd;
260
261         cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
262                               lprocfs_exp_print_nodemap_seq, m);
263         return 0;
264 }
265 LPROC_SEQ_FOPS_RO(lprocfs_exp_nodemap);
266
267 static int lprocfs_exp_uuid_seq_show(struct seq_file *m, void *data)
268 {
269         struct nid_stat *stats = m->private;
270         struct obd_device *obd = stats->nid_obd;
271
272         cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
273                                 lprocfs_exp_print_uuid_seq, m);
274         return 0;
275 }
276 LPROC_SEQ_FOPS_RO(lprocfs_exp_uuid);
277
278 static int
279 lprocfs_exp_print_hash_seq(struct cfs_hash *hs, struct cfs_hash_bd *bd,
280                            struct hlist_node *hnode, void *cb_data)
281
282 {
283         struct seq_file *m = cb_data;
284         struct obd_export *exp = cfs_hash_object(hs, hnode);
285
286         if (exp->exp_lock_hash != NULL) {
287                 cfs_hash_debug_header(m);
288                 cfs_hash_debug_str(hs, m);
289         }
290         return 0;
291 }
292
293 static int lprocfs_exp_hash_seq_show(struct seq_file *m, void *data)
294 {
295         struct nid_stat *stats = m->private;
296         struct obd_device *obd = stats->nid_obd;
297
298         cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
299                                 lprocfs_exp_print_hash_seq, m);
300         return 0;
301 }
302 LPROC_SEQ_FOPS_RO(lprocfs_exp_hash);
303
304 int lprocfs_exp_print_replydata_seq(struct cfs_hash *hs, struct cfs_hash_bd *bd,
305                                     struct hlist_node *hnode, void *cb_data)
306
307 {
308         struct obd_export *exp = cfs_hash_object(hs, hnode);
309         struct seq_file *m = cb_data;
310         struct tg_export_data *ted = &exp->exp_target_data;
311
312         seq_printf(m, "reply_cnt: %d\n"
313                    "reply_max: %d\n"
314                    "reply_released_by_xid: %d\n"
315                    "reply_released_by_tag: %d\n\n",
316                    ted->ted_reply_cnt,
317                    ted->ted_reply_max,
318                    ted->ted_release_xid,
319                    ted->ted_release_tag);
320         return 0;
321 }
322
323 int lprocfs_exp_replydata_seq_show(struct seq_file *m, void *data)
324 {
325         struct nid_stat *stats = m->private;
326         struct obd_device *obd = stats->nid_obd;
327
328         cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
329                                 lprocfs_exp_print_replydata_seq, m);
330         return 0;
331 }
332 LPROC_SEQ_FOPS_RO(lprocfs_exp_replydata);
333
334 int lprocfs_nid_stats_clear_seq_show(struct seq_file *m, void *data)
335 {
336         seq_puts(m, "Write into this file to clear all nid stats and stale nid entries\n");
337         return 0;
338 }
339 EXPORT_SYMBOL(lprocfs_nid_stats_clear_seq_show);
340
341 static int lprocfs_nid_stats_clear_write_cb(void *obj, void *data)
342 {
343         struct nid_stat *stat = obj;
344         ENTRY;
345
346         CDEBUG(D_INFO, "refcnt %d\n", atomic_read(&stat->nid_exp_ref_count));
347         if (atomic_read(&stat->nid_exp_ref_count) == 1) {
348                 /* object has only hash references. */
349                 spin_lock(&stat->nid_obd->obd_nid_lock);
350                 list_move(&stat->nid_list, data);
351                 spin_unlock(&stat->nid_obd->obd_nid_lock);
352                 RETURN(1);
353         }
354         /* we has reference to object - only clear data*/
355         if (stat->nid_stats)
356                 lprocfs_clear_stats(stat->nid_stats);
357
358         RETURN(0);
359 }
360
361 ssize_t
362 lprocfs_nid_stats_clear_seq_write(struct file *file, const char __user *buffer,
363                                         size_t count, loff_t *off)
364 {
365         struct seq_file *m = file->private_data;
366         struct obd_device *obd = m->private;
367         struct nid_stat *client_stat;
368         struct list_head free_list;
369
370         INIT_LIST_HEAD(&free_list);
371         cfs_hash_cond_del(obd->obd_nid_stats_hash,
372                           lprocfs_nid_stats_clear_write_cb, &free_list);
373
374         while (!list_empty(&free_list)) {
375                 client_stat = list_entry(free_list.next, struct nid_stat,
376                                          nid_list);
377                 list_del_init(&client_stat->nid_list);
378                 lprocfs_free_client_stats(client_stat);
379         }
380         return count;
381 }
382 EXPORT_SYMBOL(lprocfs_nid_stats_clear_seq_write);
383
384 int lprocfs_exp_setup(struct obd_export *exp, lnet_nid_t *nid)
385 {
386         struct nid_stat *new_stat, *old_stat;
387         struct obd_device *obd = NULL;
388         struct proc_dir_entry *entry;
389         char nidstr[LNET_NIDSTR_SIZE];
390         int rc = 0;
391         ENTRY;
392
393         if (!exp || !exp->exp_obd || !exp->exp_obd->obd_proc_exports_entry ||
394             !exp->exp_obd->obd_nid_stats_hash)
395                 RETURN(-EINVAL);
396
397         /* not test against zero because eric say:
398          * You may only test nid against another nid, or LNET_NID_ANY.
399          * Anything else is nonsense.*/
400         if (nid == NULL || *nid == LNET_NID_ANY)
401                 RETURN(-EALREADY);
402
403         libcfs_nid2str_r(*nid, nidstr, sizeof(nidstr));
404
405         spin_lock(&exp->exp_lock);
406         if (exp->exp_nid_stats != NULL) {
407                 spin_unlock(&exp->exp_lock);
408                 RETURN(-EALREADY);
409         }
410         spin_unlock(&exp->exp_lock);
411
412         obd = exp->exp_obd;
413
414         CDEBUG(D_CONFIG, "using hash %p\n", obd->obd_nid_stats_hash);
415
416         OBD_ALLOC_PTR(new_stat);
417         if (new_stat == NULL)
418                 RETURN(-ENOMEM);
419
420         new_stat->nid     = *nid;
421         new_stat->nid_obd = exp->exp_obd;
422         /* we need set default refcount to 1 to balance obd_disconnect */
423         atomic_set(&new_stat->nid_exp_ref_count, 1);
424
425         old_stat = cfs_hash_findadd_unique(obd->obd_nid_stats_hash,
426                                            nid, &new_stat->nid_hash);
427         CDEBUG(D_INFO, "Found stats %p for nid %s - ref %d\n",
428                old_stat, nidstr, atomic_read(&old_stat->nid_exp_ref_count));
429
430         /* Return -EALREADY here so that we know that the /proc
431          * entry already has been created */
432         if (old_stat != new_stat) {
433                 spin_lock(&exp->exp_lock);
434                 if (exp->exp_nid_stats) {
435                         LASSERT(exp->exp_nid_stats == old_stat);
436                         nidstat_putref(exp->exp_nid_stats);
437                 }
438                 exp->exp_nid_stats = old_stat;
439                 spin_unlock(&exp->exp_lock);
440                 GOTO(destroy_new, rc = -EALREADY);
441         }
442         /* not found - create */
443         new_stat->nid_proc = lprocfs_register(nidstr,
444                                               obd->obd_proc_exports_entry,
445                                               NULL, NULL);
446
447         if (IS_ERR(new_stat->nid_proc)) {
448                 rc = PTR_ERR(new_stat->nid_proc);
449                 new_stat->nid_proc = NULL;
450                 CERROR("%s: cannot create proc entry for export %s: rc = %d\n",
451                        obd->obd_name, nidstr, rc);
452                 GOTO(destroy_new_ns, rc);
453         }
454
455         entry = lprocfs_add_simple(new_stat->nid_proc, "nodemap", new_stat,
456                                    &lprocfs_exp_nodemap_fops);
457         if (IS_ERR(entry)) {
458                 rc = PTR_ERR(entry);
459                 CWARN("Error adding the nodemap file: rc = %d\n", rc);
460                 GOTO(destroy_new_ns, rc);
461         }
462
463         entry = lprocfs_add_simple(new_stat->nid_proc, "uuid", new_stat,
464                                    &lprocfs_exp_uuid_fops);
465         if (IS_ERR(entry)) {
466                 rc = PTR_ERR(entry);
467                 CWARN("Error adding the NID stats file: rc = %d\n", rc);
468                 GOTO(destroy_new_ns, rc);
469         }
470
471         entry = lprocfs_add_simple(new_stat->nid_proc, "hash", new_stat,
472                                    &lprocfs_exp_hash_fops);
473         if (IS_ERR(entry)) {
474                 rc = PTR_ERR(entry);
475                 CWARN("Error adding the hash file: rc = %d\n", rc);
476                 GOTO(destroy_new_ns, rc);
477         }
478
479         entry = lprocfs_add_simple(new_stat->nid_proc, "reply_data", new_stat,
480                                    &lprocfs_exp_replydata_fops);
481         if (IS_ERR(entry)) {
482                 rc = PTR_ERR(entry);
483                 CWARN("%s: Error adding the reply_data file: rc = %d\n",
484                       obd->obd_name, rc);
485                 GOTO(destroy_new_ns, rc);
486         }
487
488         spin_lock(&exp->exp_lock);
489         exp->exp_nid_stats = new_stat;
490         spin_unlock(&exp->exp_lock);
491
492         /* protect competitive add to list, not need locking on destroy */
493         spin_lock(&obd->obd_nid_lock);
494         list_add(&new_stat->nid_list, &obd->obd_nid_stats);
495         spin_unlock(&obd->obd_nid_lock);
496
497         RETURN(0);
498
499 destroy_new_ns:
500         if (new_stat->nid_proc != NULL)
501                 lprocfs_remove(&new_stat->nid_proc);
502         cfs_hash_del(obd->obd_nid_stats_hash, nid, &new_stat->nid_hash);
503
504 destroy_new:
505         nidstat_putref(new_stat);
506         OBD_FREE_PTR(new_stat);
507         RETURN(rc);
508 }
509 EXPORT_SYMBOL(lprocfs_exp_setup);
510
511 int lprocfs_exp_cleanup(struct obd_export *exp)
512 {
513         struct nid_stat *stat = exp->exp_nid_stats;
514
515         if (!stat || !exp->exp_obd)
516                 RETURN(0);
517
518         nidstat_putref(exp->exp_nid_stats);
519         exp->exp_nid_stats = NULL;
520
521         return 0;
522 }
523
524 #define LPROCFS_OBD_OP_INIT(base, stats, op)                    \
525 do {                                                            \
526         unsigned int coffset = base + OBD_COUNTER_OFFSET(op);   \
527         LASSERT(coffset < stats->ls_num);                       \
528         lprocfs_counter_init(stats, coffset, 0, #op, "reqs");   \
529 } while (0)
530
531 void lprocfs_init_ops_stats(int num_private_stats, struct lprocfs_stats *stats)
532 {
533         LPROCFS_OBD_OP_INIT(num_private_stats, stats, iocontrol);
534         LPROCFS_OBD_OP_INIT(num_private_stats, stats, get_info);
535         LPROCFS_OBD_OP_INIT(num_private_stats, stats, set_info_async);
536         LPROCFS_OBD_OP_INIT(num_private_stats, stats, setup);
537         LPROCFS_OBD_OP_INIT(num_private_stats, stats, precleanup);
538         LPROCFS_OBD_OP_INIT(num_private_stats, stats, cleanup);
539         LPROCFS_OBD_OP_INIT(num_private_stats, stats, process_config);
540         LPROCFS_OBD_OP_INIT(num_private_stats, stats, postrecov);
541         LPROCFS_OBD_OP_INIT(num_private_stats, stats, add_conn);
542         LPROCFS_OBD_OP_INIT(num_private_stats, stats, del_conn);
543         LPROCFS_OBD_OP_INIT(num_private_stats, stats, connect);
544         LPROCFS_OBD_OP_INIT(num_private_stats, stats, reconnect);
545         LPROCFS_OBD_OP_INIT(num_private_stats, stats, disconnect);
546         LPROCFS_OBD_OP_INIT(num_private_stats, stats, fid_init);
547         LPROCFS_OBD_OP_INIT(num_private_stats, stats, fid_fini);
548         LPROCFS_OBD_OP_INIT(num_private_stats, stats, fid_alloc);
549         LPROCFS_OBD_OP_INIT(num_private_stats, stats, statfs);
550         LPROCFS_OBD_OP_INIT(num_private_stats, stats, statfs_async);
551         LPROCFS_OBD_OP_INIT(num_private_stats, stats, create);
552         LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy);
553         LPROCFS_OBD_OP_INIT(num_private_stats, stats, setattr);
554         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr);
555         LPROCFS_OBD_OP_INIT(num_private_stats, stats, preprw);
556         LPROCFS_OBD_OP_INIT(num_private_stats, stats, commitrw);
557         LPROCFS_OBD_OP_INIT(num_private_stats, stats, init_export);
558         LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy_export);
559         LPROCFS_OBD_OP_INIT(num_private_stats, stats, import_event);
560         LPROCFS_OBD_OP_INIT(num_private_stats, stats, notify);
561         LPROCFS_OBD_OP_INIT(num_private_stats, stats, health_check);
562         LPROCFS_OBD_OP_INIT(num_private_stats, stats, get_uuid);
563         LPROCFS_OBD_OP_INIT(num_private_stats, stats, quotactl);
564         LPROCFS_OBD_OP_INIT(num_private_stats, stats, ping);
565         LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_new);
566         LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_rem);
567         LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_add);
568         LPROCFS_OBD_OP_INIT(num_private_stats, stats, pool_del);
569         LPROCFS_OBD_OP_INIT(num_private_stats, stats, getref);
570         LPROCFS_OBD_OP_INIT(num_private_stats, stats, putref);
571
572         CLASSERT(NUM_OBD_STATS == OBD_COUNTER_OFFSET(putref) + 1);
573 }
574 EXPORT_SYMBOL(lprocfs_init_ops_stats);
575
576 int lprocfs_alloc_obd_stats(struct obd_device *obd, unsigned num_private_stats)
577 {
578         struct lprocfs_stats *stats;
579         unsigned int num_stats;
580         int rc, i;
581
582         LASSERT(obd->obd_stats == NULL);
583         LASSERT(obd->obd_proc_entry != NULL);
584         LASSERT(obd->obd_cntr_base == 0);
585
586         num_stats = NUM_OBD_STATS + num_private_stats;
587         stats = lprocfs_alloc_stats(num_stats, 0);
588         if (stats == NULL)
589                 return -ENOMEM;
590
591         lprocfs_init_ops_stats(num_private_stats, stats);
592
593         for (i = num_private_stats; i < num_stats; i++) {
594                 /* If this LBUGs, it is likely that an obd
595                  * operation was added to struct obd_ops in
596                  * <obd.h>, and that the corresponding line item
597                  * LPROCFS_OBD_OP_INIT(.., .., opname)
598                  * is missing from the list above. */
599                 LASSERTF(stats->ls_cnt_header[i].lc_name != NULL,
600                          "Missing obd_stat initializer obd_op "
601                          "operation at offset %d.\n", i - num_private_stats);
602         }
603         rc = lprocfs_register_stats(obd->obd_proc_entry, "stats", stats);
604         if (rc < 0) {
605                 lprocfs_free_stats(&stats);
606         } else {
607                 obd->obd_stats  = stats;
608                 obd->obd_cntr_base = num_private_stats;
609         }
610         return rc;
611 }
612 EXPORT_SYMBOL(lprocfs_alloc_obd_stats);
613
614 void lprocfs_free_obd_stats(struct obd_device *obd)
615 {
616         if (obd->obd_stats)
617                 lprocfs_free_stats(&obd->obd_stats);
618 }
619 EXPORT_SYMBOL(lprocfs_free_obd_stats);
620
621 int lprocfs_hash_seq_show(struct seq_file *m, void *data)
622 {
623         struct obd_device *obd = m->private;
624
625         if (obd == NULL)
626                 return 0;
627
628         cfs_hash_debug_header(m);
629         cfs_hash_debug_str(obd->obd_uuid_hash, m);
630         cfs_hash_debug_str(obd->obd_nid_hash, m);
631         cfs_hash_debug_str(obd->obd_nid_stats_hash, m);
632         return 0;
633 }
634 EXPORT_SYMBOL(lprocfs_hash_seq_show);
635
636 int lprocfs_recovery_status_seq_show(struct seq_file *m, void *data)
637 {
638         struct obd_device *obd = m->private;
639         struct target_distribute_txn_data *tdtd;
640
641         LASSERT(obd != NULL);
642
643         seq_printf(m, "status: ");
644         if (obd->obd_max_recoverable_clients == 0) {
645                 seq_printf(m, "INACTIVE\n");
646                 goto out;
647         }
648
649         /* sampled unlocked, but really... */
650         if (obd->obd_recovering == 0) {
651                 seq_printf(m, "COMPLETE\n");
652                 seq_printf(m, "recovery_start: %lld\n",
653                            (s64)obd->obd_recovery_start);
654                 seq_printf(m, "recovery_duration: %lld\n",
655                            obd->obd_recovery_end ?
656                            obd->obd_recovery_end - obd->obd_recovery_start :
657                            ktime_get_real_seconds() - obd->obd_recovery_start);
658                 /* Number of clients that have completed recovery */
659                 seq_printf(m, "completed_clients: %d/%d\n",
660                            obd->obd_max_recoverable_clients -
661                            obd->obd_stale_clients,
662                            obd->obd_max_recoverable_clients);
663                 seq_printf(m, "replayed_requests: %d\n",
664                            obd->obd_replayed_requests);
665                 seq_printf(m, "last_transno: %lld\n",
666                            obd->obd_next_recovery_transno - 1);
667                 seq_printf(m, "VBR: %s\n", obd->obd_version_recov ?
668                            "ENABLED" : "DISABLED");
669                 seq_printf(m, "IR: %s\n", obd->obd_no_ir ?
670                            "DISABLED" : "ENABLED");
671                 goto out;
672         }
673
674         tdtd = obd->u.obt.obt_lut->lut_tdtd;
675         if (tdtd && tdtd->tdtd_show_update_logs_retrievers) {
676                 char *buf;
677                 int size = 0;
678                 int count = 0;
679
680                 buf = tdtd->tdtd_show_update_logs_retrievers(
681                         tdtd->tdtd_show_retrievers_cbdata,
682                         &size, &count);
683                 if (count > 0) {
684                         seq_printf(m, "WAITING\n");
685                         seq_printf(m, "non-ready MDTs: %s\n",
686                                    buf ? buf : "unknown (not enough RAM)");
687                         seq_printf(m, "recovery_start: %lld\n",
688                                    (s64)obd->obd_recovery_start);
689                         seq_printf(m, "time_waited: %lld\n",
690                                    (s64)(ktime_get_real_seconds() -
691                                          obd->obd_recovery_start));
692                 }
693
694                 if (buf != NULL)
695                         OBD_FREE(buf, size);
696
697                 if (likely(count > 0))
698                         return 0;
699         }
700
701         seq_printf(m, "RECOVERING\n");
702         seq_printf(m, "recovery_start: %lld\n", (s64)obd->obd_recovery_start);
703         seq_printf(m, "time_remaining: %lld\n",
704                    ktime_get_real_seconds() >=
705                    obd->obd_recovery_start +
706                    obd->obd_recovery_timeout ? 0 :
707                    (s64)(obd->obd_recovery_start +
708                          obd->obd_recovery_timeout -
709                          ktime_get_real_seconds()));
710         seq_printf(m, "connected_clients: %d/%d\n",
711                    atomic_read(&obd->obd_connected_clients),
712                    obd->obd_max_recoverable_clients);
713         /* Number of clients that have completed recovery */
714         seq_printf(m, "req_replay_clients: %d\n",
715                    atomic_read(&obd->obd_req_replay_clients));
716         seq_printf(m, "lock_repay_clients: %d\n",
717                    atomic_read(&obd->obd_lock_replay_clients));
718         seq_printf(m, "completed_clients: %d\n",
719                    atomic_read(&obd->obd_connected_clients) -
720                    atomic_read(&obd->obd_lock_replay_clients));
721         seq_printf(m, "evicted_clients: %d\n", obd->obd_stale_clients);
722         seq_printf(m, "replayed_requests: %d\n", obd->obd_replayed_requests);
723         seq_printf(m, "queued_requests: %d\n",
724                    obd->obd_requests_queued_for_recovery);
725         seq_printf(m, "next_transno: %lld\n",
726                    obd->obd_next_recovery_transno);
727 out:
728         return 0;
729 }
730 EXPORT_SYMBOL(lprocfs_recovery_status_seq_show);
731
732 int lprocfs_ir_factor_seq_show(struct seq_file *m, void *data)
733 {
734         struct obd_device *obd = m->private;
735
736         LASSERT(obd != NULL);
737         seq_printf(m, "%d\n", obd->obd_recovery_ir_factor);
738         return 0;
739 }
740 EXPORT_SYMBOL(lprocfs_ir_factor_seq_show);
741
742 ssize_t
743 lprocfs_ir_factor_seq_write(struct file *file, const char __user *buffer,
744                             size_t count, loff_t *off)
745 {
746         struct seq_file *m = file->private_data;
747         struct obd_device *obd = m->private;
748         int rc;
749         __s64 val;
750
751         LASSERT(obd != NULL);
752         rc = lprocfs_str_to_s64(buffer, count, &val);
753         if (rc)
754                 return rc;
755
756         if (val < OBD_IR_FACTOR_MIN || val > OBD_IR_FACTOR_MAX)
757                 return -EINVAL;
758
759         obd->obd_recovery_ir_factor = val;
760         return count;
761 }
762 EXPORT_SYMBOL(lprocfs_ir_factor_seq_write);
763
764 int lprocfs_checksum_dump_seq_show(struct seq_file *m, void *data)
765 {
766         struct obd_device *obd = m->private;
767
768         LASSERT(obd != NULL);
769         seq_printf(m, "%d\n", obd->obd_checksum_dump);
770         return 0;
771 }
772 EXPORT_SYMBOL(lprocfs_checksum_dump_seq_show);
773
774 ssize_t
775 lprocfs_checksum_dump_seq_write(struct file *file, const char __user *buffer,
776                             size_t count, loff_t *off)
777 {
778         struct seq_file *m = file->private_data;
779         struct obd_device *obd = m->private;
780         int rc;
781         __s64 val;
782
783         LASSERT(obd != NULL);
784         rc = lprocfs_str_to_s64(buffer, count, &val);
785         if (rc)
786                 return rc;
787
788         obd->obd_checksum_dump = !!val;
789         return count;
790 }
791 EXPORT_SYMBOL(lprocfs_checksum_dump_seq_write);
792
793 int lprocfs_recovery_time_soft_seq_show(struct seq_file *m, void *data)
794 {
795         struct obd_device *obd = m->private;
796
797         LASSERT(obd != NULL);
798         seq_printf(m, "%llu\n", obd->obd_recovery_timeout);
799         return 0;
800 }
801 EXPORT_SYMBOL(lprocfs_recovery_time_soft_seq_show);
802
803 ssize_t
804 lprocfs_recovery_time_soft_seq_write(struct file *file,
805                                      const char __user *buffer,
806                                      size_t count, loff_t *off)
807 {
808         struct seq_file *m = file->private_data;
809         struct obd_device *obd = m->private;
810         int rc;
811         __s64 val;
812
813         LASSERT(obd != NULL);
814         rc = lprocfs_str_to_s64(buffer, count, &val);
815         if (rc)
816                 return rc;
817         if (val < 0 || val > INT_MAX)
818                 return -ERANGE;
819
820         obd->obd_recovery_timeout = val;
821         return count;
822 }
823 EXPORT_SYMBOL(lprocfs_recovery_time_soft_seq_write);
824
825 int lprocfs_recovery_time_hard_seq_show(struct seq_file *m, void *data)
826 {
827         struct obd_device *obd = m->private;
828
829         LASSERT(obd != NULL);
830         seq_printf(m, "%lld\n", obd->obd_recovery_time_hard);
831         return 0;
832 }
833 EXPORT_SYMBOL(lprocfs_recovery_time_hard_seq_show);
834
835 ssize_t
836 lprocfs_recovery_time_hard_seq_write(struct file *file,
837                                      const char __user *buffer,
838                                      size_t count, loff_t *off)
839 {
840         struct seq_file *m = file->private_data;
841         struct obd_device *obd = m->private;
842         int rc;
843         __s64 val;
844
845         LASSERT(obd != NULL);
846         rc = lprocfs_str_to_s64(buffer, count, &val);
847         if (rc)
848                 return rc;
849         if (val < 0 || val > INT_MAX)
850                 return -ERANGE;
851
852         obd->obd_recovery_time_hard = val;
853         return count;
854 }
855 EXPORT_SYMBOL(lprocfs_recovery_time_hard_seq_write);
856
857 int lprocfs_target_instance_seq_show(struct seq_file *m, void *data)
858 {
859         struct obd_device *obd = m->private;
860         struct obd_device_target *target = &obd->u.obt;
861
862         LASSERT(obd != NULL);
863         LASSERT(target->obt_magic == OBT_MAGIC);
864         seq_printf(m, "%u\n", obd->u.obt.obt_instance);
865         return 0;
866 }
867 EXPORT_SYMBOL(lprocfs_target_instance_seq_show);
868
869 #endif /* CONFIG_PROC_FS*/