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