]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/lustre/lustre/obdclass/lprocfs_status.c
51fe15f5d6870fe26987120f88f35d8477a83deb
[karo-tx-linux.git] / drivers / staging / lustre / lustre / obdclass / lprocfs_status.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/lprocfs_status.c
37  *
38  * Author: Hariharan Thantry <thantry@users.sourceforge.net>
39  */
40
41 #define DEBUG_SUBSYSTEM S_CLASS
42
43 #include "../include/obd_class.h"
44 #include "../include/lprocfs_status.h"
45 #include "../include/lustre/lustre_idl.h"
46 #include <linux/seq_file.h>
47 #include <linux/ctype.h>
48
49 static const char * const obd_connect_names[] = {
50         "read_only",
51         "lov_index",
52         "unused",
53         "write_grant",
54         "server_lock",
55         "version",
56         "request_portal",
57         "acl",
58         "xattr",
59         "create_on_write",
60         "truncate_lock",
61         "initial_transno",
62         "inode_bit_locks",
63         "join_file(obsolete)",
64         "getattr_by_fid",
65         "no_oh_for_devices",
66         "remote_client",
67         "remote_client_by_force",
68         "max_byte_per_rpc",
69         "64bit_qdata",
70         "mds_capability",
71         "oss_capability",
72         "early_lock_cancel",
73         "som",
74         "adaptive_timeouts",
75         "lru_resize",
76         "mds_mds_connection",
77         "real_conn",
78         "change_qunit_size",
79         "alt_checksum_algorithm",
80         "fid_is_enabled",
81         "version_recovery",
82         "pools",
83         "grant_shrink",
84         "skip_orphan",
85         "large_ea",
86         "full20",
87         "layout_lock",
88         "64bithash",
89         "object_max_bytes",
90         "imp_recov",
91         "jobstats",
92         "umask",
93         "einprogress",
94         "grant_param",
95         "flock_owner",
96         "lvb_type",
97         "nanoseconds_times",
98         "lightweight_conn",
99         "short_io",
100         "pingless",
101         "flock_deadlock",
102         "disp_stripe",
103         "unknown",
104         NULL
105 };
106
107 int obd_connect_flags2str(char *page, int count, __u64 flags, char *sep)
108 {
109         __u64 mask = 1;
110         int i, ret = 0;
111
112         for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
113                 if (flags & mask)
114                         ret += snprintf(page + ret, count - ret, "%s%s",
115                                         ret ? sep : "", obd_connect_names[i]);
116         }
117         if (flags & ~(mask - 1))
118                 ret += snprintf(page + ret, count - ret,
119                                 "%sunknown flags %#llx",
120                                 ret ? sep : "", flags & ~(mask - 1));
121         return ret;
122 }
123 EXPORT_SYMBOL(obd_connect_flags2str);
124
125 int lprocfs_read_frac_helper(char *buffer, unsigned long count, long val,
126                              int mult)
127 {
128         long decimal_val, frac_val;
129         int prtn;
130
131         if (count < 10)
132                 return -EINVAL;
133
134         decimal_val = val / mult;
135         prtn = snprintf(buffer, count, "%ld", decimal_val);
136         frac_val = val % mult;
137
138         if (prtn < (count - 4) && frac_val > 0) {
139                 long temp_frac;
140                 int i, temp_mult = 1, frac_bits = 0;
141
142                 temp_frac = frac_val * 10;
143                 buffer[prtn++] = '.';
144                 while (frac_bits < 2 && (temp_frac / mult) < 1) {
145                         /* only reserved 2 bits fraction */
146                         buffer[prtn++] = '0';
147                         temp_frac *= 10;
148                         frac_bits++;
149                 }
150                 /*
151                  * Need to think these cases :
152                  *      1. #echo x.00 > /proc/xxx       output result : x
153                  *      2. #echo x.0x > /proc/xxx       output result : x.0x
154                  *      3. #echo x.x0 > /proc/xxx       output result : x.x
155                  *      4. #echo x.xx > /proc/xxx       output result : x.xx
156                  *      Only reserved 2 bits fraction.
157                  */
158                 for (i = 0; i < (5 - prtn); i++)
159                         temp_mult *= 10;
160
161                 frac_bits = min((int)count - prtn, 3 - frac_bits);
162                 prtn += snprintf(buffer + prtn, frac_bits, "%ld",
163                                  frac_val * temp_mult / mult);
164
165                 prtn--;
166                 while (buffer[prtn] < '1' || buffer[prtn] > '9') {
167                         prtn--;
168                         if (buffer[prtn] == '.') {
169                                 prtn--;
170                                 break;
171                         }
172                 }
173                 prtn++;
174         }
175         buffer[prtn++] = '\n';
176         return prtn;
177 }
178 EXPORT_SYMBOL(lprocfs_read_frac_helper);
179
180 int lprocfs_write_frac_helper(const char __user *buffer, unsigned long count,
181                               int *val, int mult)
182 {
183         char kernbuf[20], *end, *pbuf;
184
185         if (count > (sizeof(kernbuf) - 1))
186                 return -EINVAL;
187
188         if (copy_from_user(kernbuf, buffer, count))
189                 return -EFAULT;
190
191         kernbuf[count] = '\0';
192         pbuf = kernbuf;
193         if (*pbuf == '-') {
194                 mult = -mult;
195                 pbuf++;
196         }
197
198         *val = (int)simple_strtoul(pbuf, &end, 10) * mult;
199         if (pbuf == end)
200                 return -EINVAL;
201
202         if (end != NULL && *end == '.') {
203                 int temp_val, pow = 1;
204                 int i;
205
206                 pbuf = end + 1;
207                 if (strlen(pbuf) > 5)
208                         pbuf[5] = '\0'; /*only allow 5bits fractional*/
209
210                 temp_val = (int)simple_strtoul(pbuf, &end, 10) * mult;
211
212                 if (pbuf < end) {
213                         for (i = 0; i < (end - pbuf); i++)
214                                 pow *= 10;
215
216                         *val += temp_val / pow;
217                 }
218         }
219         return 0;
220 }
221 EXPORT_SYMBOL(lprocfs_write_frac_helper);
222
223 static int lprocfs_no_percpu_stats;
224 module_param(lprocfs_no_percpu_stats, int, 0644);
225 MODULE_PARM_DESC(lprocfs_no_percpu_stats, "Do not alloc percpu data for lprocfs stats");
226
227 #define MAX_STRING_SIZE 128
228
229 int lprocfs_single_release(struct inode *inode, struct file *file)
230 {
231         return single_release(inode, file);
232 }
233 EXPORT_SYMBOL(lprocfs_single_release);
234
235 int lprocfs_seq_release(struct inode *inode, struct file *file)
236 {
237         return seq_release(inode, file);
238 }
239 EXPORT_SYMBOL(lprocfs_seq_release);
240
241 /* lprocfs API calls */
242
243 struct dentry *ldebugfs_add_simple(struct dentry *root,
244                                    char *name, void *data,
245                                    struct file_operations *fops)
246 {
247         struct dentry *entry;
248         umode_t mode = 0;
249
250         if (root == NULL || name == NULL || fops == NULL)
251                 return ERR_PTR(-EINVAL);
252
253         if (fops->read)
254                 mode = 0444;
255         if (fops->write)
256                 mode |= 0200;
257         entry = debugfs_create_file(name, mode, root, data, fops);
258         if (IS_ERR_OR_NULL(entry)) {
259                 CERROR("LprocFS: No memory to create <debugfs> entry %s", name);
260                 return entry ?: ERR_PTR(-ENOMEM);
261         }
262         return entry;
263 }
264 EXPORT_SYMBOL(ldebugfs_add_simple);
265
266 static struct file_operations lprocfs_generic_fops = { };
267
268 int ldebugfs_add_vars(struct dentry *parent,
269                       struct lprocfs_vars *list,
270                       void *data)
271 {
272         if (IS_ERR_OR_NULL(parent) || IS_ERR_OR_NULL(list))
273                 return -EINVAL;
274
275         while (list->name != NULL) {
276                 struct dentry *entry;
277                 umode_t mode = 0;
278
279                 if (list->proc_mode != 0000) {
280                         mode = list->proc_mode;
281                 } else if (list->fops) {
282                         if (list->fops->read)
283                                 mode = 0444;
284                         if (list->fops->write)
285                                 mode |= 0200;
286                 }
287                 entry = debugfs_create_file(list->name, mode, parent,
288                                             list->data ?: data,
289                                             list->fops ?: &lprocfs_generic_fops
290                                            );
291                 if (IS_ERR_OR_NULL(entry))
292                         return entry ? PTR_ERR(entry) : -ENOMEM;
293                 list++;
294         }
295         return 0;
296 }
297 EXPORT_SYMBOL(ldebugfs_add_vars);
298
299 void ldebugfs_remove(struct dentry **entryp)
300 {
301         debugfs_remove_recursive(*entryp);
302         *entryp = NULL;
303 }
304 EXPORT_SYMBOL(ldebugfs_remove);
305
306 struct dentry *ldebugfs_register(const char *name,
307                                  struct dentry *parent,
308                                  struct lprocfs_vars *list, void *data)
309 {
310         struct dentry *entry;
311
312         entry = debugfs_create_dir(name, parent);
313         if (IS_ERR_OR_NULL(entry)) {
314                 entry = entry ?: ERR_PTR(-ENOMEM);
315                 goto out;
316         }
317
318         if (!IS_ERR_OR_NULL(list)) {
319                 int rc;
320
321                 rc = ldebugfs_add_vars(entry, list, data);
322                 if (rc != 0) {
323                         debugfs_remove(entry);
324                         entry = ERR_PTR(rc);
325                 }
326         }
327 out:
328         return entry;
329 }
330 EXPORT_SYMBOL(ldebugfs_register);
331
332 /* Generic callbacks */
333 int lprocfs_rd_uint(struct seq_file *m, void *data)
334 {
335         seq_printf(m, "%u\n", *(unsigned int *)data);
336         return 0;
337 }
338 EXPORT_SYMBOL(lprocfs_rd_uint);
339
340 int lprocfs_wr_uint(struct file *file, const char __user *buffer,
341                     unsigned long count, void *data)
342 {
343         unsigned *p = data;
344         char dummy[MAX_STRING_SIZE + 1], *end;
345         unsigned long tmp;
346
347         dummy[MAX_STRING_SIZE] = '\0';
348         if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
349                 return -EFAULT;
350
351         tmp = simple_strtoul(dummy, &end, 0);
352         if (dummy == end)
353                 return -EINVAL;
354
355         *p = (unsigned int)tmp;
356         return count;
357 }
358 EXPORT_SYMBOL(lprocfs_wr_uint);
359
360 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
361                          char *buf)
362 {
363         struct obd_device *obd = container_of(kobj, struct obd_device,
364                                               obd_kobj);
365
366         return sprintf(buf, "%s\n", obd->obd_uuid.uuid);
367 }
368 LUSTRE_RO_ATTR(uuid);
369
370 static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
371                               char *buf)
372 {
373         struct obd_device *obd = container_of(kobj, struct obd_device,
374                                               obd_kobj);
375         struct obd_statfs  osfs;
376         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
377                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
378                             OBD_STATFS_NODELAY);
379         if (!rc)
380                 return sprintf(buf, "%u\n", osfs.os_bsize);
381
382         return rc;
383 }
384 LUSTRE_RO_ATTR(blocksize);
385
386 static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
387                                 char *buf)
388 {
389         struct obd_device *obd = container_of(kobj, struct obd_device,
390                                               obd_kobj);
391         struct obd_statfs  osfs;
392         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
393                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
394                             OBD_STATFS_NODELAY);
395         if (!rc) {
396                 __u32 blk_size = osfs.os_bsize >> 10;
397                 __u64 result = osfs.os_blocks;
398
399                 while (blk_size >>= 1)
400                         result <<= 1;
401
402                 return sprintf(buf, "%llu\n", result);
403         }
404
405         return rc;
406 }
407 LUSTRE_RO_ATTR(kbytestotal);
408
409 static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
410                                char *buf)
411 {
412         struct obd_device *obd = container_of(kobj, struct obd_device,
413                                               obd_kobj);
414         struct obd_statfs  osfs;
415         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
416                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
417                             OBD_STATFS_NODELAY);
418         if (!rc) {
419                 __u32 blk_size = osfs.os_bsize >> 10;
420                 __u64 result = osfs.os_bfree;
421
422                 while (blk_size >>= 1)
423                         result <<= 1;
424
425                 return sprintf(buf, "%llu\n", result);
426         }
427
428         return rc;
429 }
430 LUSTRE_RO_ATTR(kbytesfree);
431
432 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
433                                 char *buf)
434 {
435         struct obd_device *obd = container_of(kobj, struct obd_device,
436                                               obd_kobj);
437         struct obd_statfs  osfs;
438         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
439                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
440                             OBD_STATFS_NODELAY);
441         if (!rc) {
442                 __u32 blk_size = osfs.os_bsize >> 10;
443                 __u64 result = osfs.os_bavail;
444
445                 while (blk_size >>= 1)
446                         result <<= 1;
447
448                 return sprintf(buf, "%llu\n", result);
449         }
450
451         return rc;
452 }
453 LUSTRE_RO_ATTR(kbytesavail);
454
455 static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
456                                char *buf)
457 {
458         struct obd_device *obd = container_of(kobj, struct obd_device,
459                                               obd_kobj);
460         struct obd_statfs  osfs;
461         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
462                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
463                             OBD_STATFS_NODELAY);
464         if (!rc)
465                 return sprintf(buf, "%llu\n", osfs.os_files);
466
467         return rc;
468 }
469 LUSTRE_RO_ATTR(filestotal);
470
471 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
472                               char *buf)
473 {
474         struct obd_device *obd = container_of(kobj, struct obd_device,
475                                               obd_kobj);
476         struct obd_statfs  osfs;
477         int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
478                             cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
479                             OBD_STATFS_NODELAY);
480         if (!rc)
481                 return sprintf(buf, "%llu\n", osfs.os_ffree);
482
483         return rc;
484 }
485 LUSTRE_RO_ATTR(filesfree);
486
487 int lprocfs_rd_server_uuid(struct seq_file *m, void *data)
488 {
489         struct obd_device *obd = data;
490         struct obd_import *imp;
491         char *imp_state_name = NULL;
492         int rc;
493
494         LASSERT(obd != NULL);
495         rc = lprocfs_climp_check(obd);
496         if (rc)
497                 return rc;
498
499         imp = obd->u.cli.cl_import;
500         imp_state_name = ptlrpc_import_state_name(imp->imp_state);
501         seq_printf(m, "%s\t%s%s\n",
502                    obd2cli_tgt(obd), imp_state_name,
503                    imp->imp_deactive ? "\tDEACTIVATED" : "");
504
505         up_read(&obd->u.cli.cl_sem);
506
507         return 0;
508 }
509 EXPORT_SYMBOL(lprocfs_rd_server_uuid);
510
511 int lprocfs_rd_conn_uuid(struct seq_file *m, void *data)
512 {
513         struct obd_device *obd = data;
514         struct ptlrpc_connection *conn;
515         int rc;
516
517         LASSERT(obd != NULL);
518
519         rc = lprocfs_climp_check(obd);
520         if (rc)
521                 return rc;
522
523         conn = obd->u.cli.cl_import->imp_connection;
524         if (conn && obd->u.cli.cl_import)
525                 seq_printf(m, "%s\n", conn->c_remote_uuid.uuid);
526         else
527                 seq_puts(m, "<none>\n");
528
529         up_read(&obd->u.cli.cl_sem);
530
531         return 0;
532 }
533 EXPORT_SYMBOL(lprocfs_rd_conn_uuid);
534
535 /** add up per-cpu counters */
536 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
537                            struct lprocfs_counter *cnt)
538 {
539         unsigned int                    num_entry;
540         struct lprocfs_counter          *percpu_cntr;
541         int                             i;
542         unsigned long                   flags = 0;
543
544         memset(cnt, 0, sizeof(*cnt));
545
546         if (stats == NULL) {
547                 /* set count to 1 to avoid divide-by-zero errs in callers */
548                 cnt->lc_count = 1;
549                 return;
550         }
551
552         cnt->lc_min = LC_MIN_INIT;
553
554         num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
555
556         for (i = 0; i < num_entry; i++) {
557                 if (stats->ls_percpu[i] == NULL)
558                         continue;
559                 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
560
561                 cnt->lc_count += percpu_cntr->lc_count;
562                 cnt->lc_sum += percpu_cntr->lc_sum;
563                 if (percpu_cntr->lc_min < cnt->lc_min)
564                         cnt->lc_min = percpu_cntr->lc_min;
565                 if (percpu_cntr->lc_max > cnt->lc_max)
566                         cnt->lc_max = percpu_cntr->lc_max;
567                 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
568         }
569
570         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
571 }
572 EXPORT_SYMBOL(lprocfs_stats_collect);
573
574 /**
575  * Append a space separated list of current set flags to str.
576  */
577 #define flag2str(flag, first)                                           \
578         do {                                                            \
579                 if (imp->imp_##flag)                                    \
580                      seq_printf(m, "%s" #flag, first ? "" : ", ");      \
581         } while (0)
582 static int obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
583 {
584         bool first = true;
585
586         if (imp->imp_obd->obd_no_recov) {
587                 seq_printf(m, "no_recov");
588                 first = false;
589         }
590
591         flag2str(invalid, first);
592         first = false;
593         flag2str(deactive, first);
594         flag2str(replayable, first);
595         flag2str(pingable, first);
596         return 0;
597 }
598
599 #undef flags2str
600
601 static void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, char *sep)
602 {
603         __u64 mask = 1;
604         int i;
605         bool first = true;
606
607         for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
608                 if (flags & mask) {
609                         seq_printf(m, "%s%s",
610                                         first ? sep : "", obd_connect_names[i]);
611                         first = false;
612                 }
613         }
614         if (flags & ~(mask - 1))
615                 seq_printf(m, "%sunknown flags %#llx",
616                                 first ? sep : "", flags & ~(mask - 1));
617 }
618
619 int lprocfs_rd_import(struct seq_file *m, void *data)
620 {
621         char                            nidstr[LNET_NIDSTR_SIZE];
622         struct lprocfs_counter          ret;
623         struct lprocfs_counter_header   *header;
624         struct obd_device               *obd    = data;
625         struct obd_import               *imp;
626         struct obd_import_conn          *conn;
627         int                             j;
628         int                             k;
629         int                             rw      = 0;
630         int                             rc;
631
632         LASSERT(obd != NULL);
633         rc = lprocfs_climp_check(obd);
634         if (rc)
635                 return rc;
636
637         imp = obd->u.cli.cl_import;
638
639         seq_printf(m,
640                      "import:\n"
641                      "    name: %s\n"
642                      "    target: %s\n"
643                      "    state: %s\n"
644                      "    instance: %u\n"
645                      "    connect_flags: [",
646                      obd->obd_name,
647                      obd2cli_tgt(obd),
648                      ptlrpc_import_state_name(imp->imp_state),
649                      imp->imp_connect_data.ocd_instance);
650         obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags, ", ");
651         seq_printf(m,
652                       "]\n"
653                       "    import_flags: [");
654         obd_import_flags2str(imp, m);
655
656         seq_printf(m,
657                       "]\n"
658                       "    connection:\n"
659                       "       failover_nids: [");
660         spin_lock(&imp->imp_lock);
661         j = 0;
662         list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
663                 libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
664                                  nidstr, sizeof(nidstr));
665                 seq_printf(m, "%s%s", j ? ", " : "", nidstr);
666                 j++;
667         }
668         if (imp->imp_connection != NULL)
669                 libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
670                                  nidstr, sizeof(nidstr));
671         else
672                 strncpy(nidstr, "<none>", sizeof(nidstr));
673         seq_printf(m,
674                       "]\n"
675                       "       current_connection: %s\n"
676                       "       connection_attempts: %u\n"
677                       "       generation: %u\n"
678                       "       in-progress_invalidations: %u\n",
679                       nidstr,
680                       imp->imp_conn_cnt,
681                       imp->imp_generation,
682                       atomic_read(&imp->imp_inval_count));
683         spin_unlock(&imp->imp_lock);
684
685         if (obd->obd_svc_stats == NULL)
686                 goto out_climp;
687
688         header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
689         lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
690         if (ret.lc_count != 0) {
691                 /* first argument to do_div MUST be __u64 */
692                 __u64 sum = ret.lc_sum;
693
694                 do_div(sum, ret.lc_count);
695                 ret.lc_sum = sum;
696         } else
697                 ret.lc_sum = 0;
698         seq_printf(m,
699                       "    rpcs:\n"
700                       "       inflight: %u\n"
701                       "       unregistering: %u\n"
702                       "       timeouts: %u\n"
703                       "       avg_waittime: %llu %s\n",
704                       atomic_read(&imp->imp_inflight),
705                       atomic_read(&imp->imp_unregistering),
706                       atomic_read(&imp->imp_timeouts),
707                       ret.lc_sum, header->lc_units);
708
709         k = 0;
710         for (j = 0; j < IMP_AT_MAX_PORTALS; j++) {
711                 if (imp->imp_at.iat_portal[j] == 0)
712                         break;
713                 k = max_t(unsigned int, k,
714                           at_get(&imp->imp_at.iat_service_estimate[j]));
715         }
716         seq_printf(m,
717                       "    service_estimates:\n"
718                       "       services: %u sec\n"
719                       "       network: %u sec\n",
720                       k,
721                       at_get(&imp->imp_at.iat_net_latency));
722
723         seq_printf(m,
724                       "    transactions:\n"
725                       "       last_replay: %llu\n"
726                       "       peer_committed: %llu\n"
727                       "       last_checked: %llu\n",
728                       imp->imp_last_replay_transno,
729                       imp->imp_peer_committed_transno,
730                       imp->imp_last_transno_checked);
731
732         /* avg data rates */
733         for (rw = 0; rw <= 1; rw++) {
734                 lprocfs_stats_collect(obd->obd_svc_stats,
735                                       PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
736                                       &ret);
737                 if (ret.lc_sum > 0 && ret.lc_count > 0) {
738                         /* first argument to do_div MUST be __u64 */
739                         __u64 sum = ret.lc_sum;
740
741                         do_div(sum, ret.lc_count);
742                         ret.lc_sum = sum;
743                         seq_printf(m,
744                                       "    %s_data_averages:\n"
745                                       "       bytes_per_rpc: %llu\n",
746                                       rw ? "write" : "read",
747                                       ret.lc_sum);
748                 }
749                 k = (int)ret.lc_sum;
750                 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
751                 header = &obd->obd_svc_stats->ls_cnt_header[j];
752                 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
753                 if (ret.lc_sum > 0 && ret.lc_count != 0) {
754                         /* first argument to do_div MUST be __u64 */
755                         __u64 sum = ret.lc_sum;
756
757                         do_div(sum, ret.lc_count);
758                         ret.lc_sum = sum;
759                         seq_printf(m,
760                                       "       %s_per_rpc: %llu\n",
761                                       header->lc_units, ret.lc_sum);
762                         j = (int)ret.lc_sum;
763                         if (j > 0)
764                                 seq_printf(m,
765                                               "       MB_per_sec: %u.%.02u\n",
766                                               k / j, (100 * k / j) % 100);
767                 }
768         }
769
770 out_climp:
771         up_read(&obd->u.cli.cl_sem);
772         return 0;
773 }
774 EXPORT_SYMBOL(lprocfs_rd_import);
775
776 int lprocfs_rd_state(struct seq_file *m, void *data)
777 {
778         struct obd_device *obd = data;
779         struct obd_import *imp;
780         int j, k, rc;
781
782         LASSERT(obd != NULL);
783         rc = lprocfs_climp_check(obd);
784         if (rc)
785                 return rc;
786
787         imp = obd->u.cli.cl_import;
788
789         seq_printf(m, "current_state: %s\n",
790                      ptlrpc_import_state_name(imp->imp_state));
791         seq_printf(m, "state_history:\n");
792         k = imp->imp_state_hist_idx;
793         for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
794                 struct import_state_hist *ish =
795                         &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
796                 if (ish->ish_state == 0)
797                         continue;
798                 seq_printf(m, " - [%lld, %s]\n", (s64)ish->ish_time,
799                            ptlrpc_import_state_name(ish->ish_state));
800         }
801
802         up_read(&obd->u.cli.cl_sem);
803         return 0;
804 }
805 EXPORT_SYMBOL(lprocfs_rd_state);
806
807 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
808 {
809         int i;
810
811         for (i = 0; i < AT_BINS; i++)
812                 seq_printf(m, "%3u ", at->at_hist[i]);
813         seq_printf(m, "\n");
814         return 0;
815 }
816 EXPORT_SYMBOL(lprocfs_at_hist_helper);
817
818 /* See also ptlrpc_lprocfs_rd_timeouts */
819 int lprocfs_rd_timeouts(struct seq_file *m, void *data)
820 {
821         struct obd_device *obd = data;
822         struct obd_import *imp;
823         unsigned int cur, worst;
824         time64_t now, worstt;
825         struct dhms ts;
826         int i, rc;
827
828         LASSERT(obd != NULL);
829         rc = lprocfs_climp_check(obd);
830         if (rc)
831                 return rc;
832
833         imp = obd->u.cli.cl_import;
834
835         now = ktime_get_real_seconds();
836
837         /* Some network health info for kicks */
838         s2dhms(&ts, now - imp->imp_last_reply_time);
839         seq_printf(m, "%-10s : %lld, " DHMS_FMT " ago\n",
840                    "last reply", (s64)imp->imp_last_reply_time, DHMS_VARS(&ts));
841
842         cur = at_get(&imp->imp_at.iat_net_latency);
843         worst = imp->imp_at.iat_net_latency.at_worst_ever;
844         worstt = imp->imp_at.iat_net_latency.at_worst_time;
845         s2dhms(&ts, now - worstt);
846         seq_printf(m, "%-10s : cur %3u  worst %3u (at %lld, " DHMS_FMT " ago) ",
847                    "network", cur, worst, (s64)worstt, DHMS_VARS(&ts));
848         lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
849
850         for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
851                 if (imp->imp_at.iat_portal[i] == 0)
852                         break;
853                 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
854                 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
855                 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
856                 s2dhms(&ts, now - worstt);
857                 seq_printf(m, "portal %-2d  : cur %3u  worst %3u (at %lld, "
858                            DHMS_FMT " ago) ", imp->imp_at.iat_portal[i],
859                            cur, worst, (s64)worstt, DHMS_VARS(&ts));
860                 lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
861         }
862
863         up_read(&obd->u.cli.cl_sem);
864         return 0;
865 }
866 EXPORT_SYMBOL(lprocfs_rd_timeouts);
867
868 int lprocfs_rd_connect_flags(struct seq_file *m, void *data)
869 {
870         struct obd_device *obd = data;
871         __u64 flags;
872         int rc;
873
874         rc = lprocfs_climp_check(obd);
875         if (rc)
876                 return rc;
877
878         flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
879         seq_printf(m, "flags=%#llx\n", flags);
880         obd_connect_seq_flags2str(m, flags, "\n");
881         seq_printf(m, "\n");
882         up_read(&obd->u.cli.cl_sem);
883         return 0;
884 }
885 EXPORT_SYMBOL(lprocfs_rd_connect_flags);
886
887 static struct attribute *obd_def_attrs[] = {
888         &lustre_attr_blocksize.attr,
889         &lustre_attr_kbytestotal.attr,
890         &lustre_attr_kbytesfree.attr,
891         &lustre_attr_kbytesavail.attr,
892         &lustre_attr_filestotal.attr,
893         &lustre_attr_filesfree.attr,
894         &lustre_attr_uuid.attr,
895         NULL,
896 };
897
898 static void obd_sysfs_release(struct kobject *kobj)
899 {
900         struct obd_device *obd = container_of(kobj, struct obd_device,
901                                               obd_kobj);
902
903         complete(&obd->obd_kobj_unregister);
904 }
905
906 static struct kobj_type obd_ktype = {
907         .default_attrs  = obd_def_attrs,
908         .sysfs_ops      = &lustre_sysfs_ops,
909         .release        = obd_sysfs_release,
910 };
911
912 int lprocfs_obd_setup(struct obd_device *obd, struct lprocfs_vars *list,
913                       struct attribute_group *attrs)
914 {
915         int rc = 0;
916
917         init_completion(&obd->obd_kobj_unregister);
918         rc = kobject_init_and_add(&obd->obd_kobj, &obd_ktype,
919                                   obd->obd_type->typ_kobj,
920                                   "%s", obd->obd_name);
921         if (rc)
922                 return rc;
923
924         if (attrs) {
925                 rc = sysfs_create_group(&obd->obd_kobj, attrs);
926                 if (rc) {
927                         kobject_put(&obd->obd_kobj);
928                         return rc;
929                 }
930         }
931
932         obd->obd_debugfs_entry = ldebugfs_register(obd->obd_name,
933                                                    obd->obd_type->typ_debugfs_entry,
934                                                    list, obd);
935         if (IS_ERR_OR_NULL(obd->obd_debugfs_entry)) {
936                 rc = obd->obd_debugfs_entry ? PTR_ERR(obd->obd_debugfs_entry)
937                                             : -ENOMEM;
938                 CERROR("error %d setting up lprocfs for %s\n",
939                        rc, obd->obd_name);
940                 obd->obd_debugfs_entry = NULL;
941         }
942
943         return rc;
944 }
945 EXPORT_SYMBOL(lprocfs_obd_setup);
946
947 int lprocfs_obd_cleanup(struct obd_device *obd)
948 {
949         if (!obd)
950                 return -EINVAL;
951
952         if (!IS_ERR_OR_NULL(obd->obd_debugfs_entry))
953                 ldebugfs_remove(&obd->obd_debugfs_entry);
954
955         kobject_put(&obd->obd_kobj);
956         wait_for_completion(&obd->obd_kobj_unregister);
957
958         return 0;
959 }
960 EXPORT_SYMBOL(lprocfs_obd_cleanup);
961
962 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
963 {
964         struct lprocfs_counter  *cntr;
965         unsigned int            percpusize;
966         int                     rc = -ENOMEM;
967         unsigned long           flags = 0;
968         int                     i;
969
970         LASSERT(stats->ls_percpu[cpuid] == NULL);
971         LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
972
973         percpusize = lprocfs_stats_counter_size(stats);
974         LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
975         if (stats->ls_percpu[cpuid] != NULL) {
976                 rc = 0;
977                 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
978                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
979                                 spin_lock_irqsave(&stats->ls_lock, flags);
980                         else
981                                 spin_lock(&stats->ls_lock);
982                         if (stats->ls_biggest_alloc_num <= cpuid)
983                                 stats->ls_biggest_alloc_num = cpuid + 1;
984                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
985                                 spin_unlock_irqrestore(&stats->ls_lock, flags);
986                         else
987                                 spin_unlock(&stats->ls_lock);
988                 }
989                 /* initialize the ls_percpu[cpuid] non-zero counter */
990                 for (i = 0; i < stats->ls_num; ++i) {
991                         cntr = lprocfs_stats_counter_get(stats, cpuid, i);
992                         cntr->lc_min = LC_MIN_INIT;
993                 }
994         }
995         return rc;
996 }
997 EXPORT_SYMBOL(lprocfs_stats_alloc_one);
998
999 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1000                                           enum lprocfs_stats_flags flags)
1001 {
1002         struct lprocfs_stats    *stats;
1003         unsigned int            num_entry;
1004         unsigned int            percpusize = 0;
1005         int                     i;
1006
1007         if (num == 0)
1008                 return NULL;
1009
1010         if (lprocfs_no_percpu_stats != 0)
1011                 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1012
1013         if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1014                 num_entry = 1;
1015         else
1016                 num_entry = num_possible_cpus();
1017
1018         /* alloc percpu pointers for all possible cpu slots */
1019         LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1020         if (stats == NULL)
1021                 return NULL;
1022
1023         stats->ls_num = num;
1024         stats->ls_flags = flags;
1025         spin_lock_init(&stats->ls_lock);
1026
1027         /* alloc num of counter headers */
1028         LIBCFS_ALLOC(stats->ls_cnt_header,
1029                      stats->ls_num * sizeof(struct lprocfs_counter_header));
1030         if (stats->ls_cnt_header == NULL)
1031                 goto fail;
1032
1033         if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1034                 /* contains only one set counters */
1035                 percpusize = lprocfs_stats_counter_size(stats);
1036                 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1037                 if (stats->ls_percpu[0] == NULL)
1038                         goto fail;
1039                 stats->ls_biggest_alloc_num = 1;
1040         } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1041                 /* alloc all percpu data */
1042                 for (i = 0; i < num_entry; ++i)
1043                         if (lprocfs_stats_alloc_one(stats, i) < 0)
1044                                 goto fail;
1045         }
1046
1047         return stats;
1048
1049 fail:
1050         lprocfs_free_stats(&stats);
1051         return NULL;
1052 }
1053 EXPORT_SYMBOL(lprocfs_alloc_stats);
1054
1055 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1056 {
1057         struct lprocfs_stats *stats = *statsh;
1058         unsigned int num_entry;
1059         unsigned int percpusize;
1060         unsigned int i;
1061
1062         if (stats == NULL || stats->ls_num == 0)
1063                 return;
1064         *statsh = NULL;
1065
1066         if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1067                 num_entry = 1;
1068         else
1069                 num_entry = num_possible_cpus();
1070
1071         percpusize = lprocfs_stats_counter_size(stats);
1072         for (i = 0; i < num_entry; i++)
1073                 if (stats->ls_percpu[i] != NULL)
1074                         LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1075         if (stats->ls_cnt_header != NULL)
1076                 LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
1077                                         sizeof(struct lprocfs_counter_header));
1078         LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1079 }
1080 EXPORT_SYMBOL(lprocfs_free_stats);
1081
1082 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1083 {
1084         struct lprocfs_counter          *percpu_cntr;
1085         int                             i;
1086         int                             j;
1087         unsigned int                    num_entry;
1088         unsigned long                   flags = 0;
1089
1090         num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1091
1092         for (i = 0; i < num_entry; i++) {
1093                 if (stats->ls_percpu[i] == NULL)
1094                         continue;
1095                 for (j = 0; j < stats->ls_num; j++) {
1096                         percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1097                         percpu_cntr->lc_count           = 0;
1098                         percpu_cntr->lc_min             = LC_MIN_INIT;
1099                         percpu_cntr->lc_max             = 0;
1100                         percpu_cntr->lc_sumsquare       = 0;
1101                         percpu_cntr->lc_sum             = 0;
1102                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1103                                 percpu_cntr->lc_sum_irq = 0;
1104                 }
1105         }
1106
1107         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1108 }
1109 EXPORT_SYMBOL(lprocfs_clear_stats);
1110
1111 static ssize_t lprocfs_stats_seq_write(struct file *file,
1112                                        const char __user *buf,
1113                                        size_t len, loff_t *off)
1114 {
1115         struct seq_file *seq = file->private_data;
1116         struct lprocfs_stats *stats = seq->private;
1117
1118         lprocfs_clear_stats(stats);
1119
1120         return len;
1121 }
1122
1123 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1124 {
1125         struct lprocfs_stats *stats = p->private;
1126
1127         return (*pos < stats->ls_num) ? pos : NULL;
1128 }
1129
1130 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1131 {
1132 }
1133
1134 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1135 {
1136         (*pos)++;
1137         return lprocfs_stats_seq_start(p, pos);
1138 }
1139
1140 /* seq file export of one lprocfs counter */
1141 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1142 {
1143         struct lprocfs_stats            *stats  = p->private;
1144         struct lprocfs_counter_header   *hdr;
1145         struct lprocfs_counter           ctr;
1146         int                              idx    = *(loff_t *)v;
1147
1148         if (idx == 0) {
1149                 struct timespec64 now;
1150
1151                 ktime_get_real_ts64(&now);
1152                 seq_printf(p, "%-25s %llu.%9lu secs.usecs\n",
1153                            "snapshot_time",
1154                            (s64)now.tv_sec, (unsigned long)now.tv_nsec);
1155         }
1156
1157         hdr = &stats->ls_cnt_header[idx];
1158         lprocfs_stats_collect(stats, idx, &ctr);
1159
1160         if (ctr.lc_count != 0) {
1161                 seq_printf(p, "%-25s %lld samples [%s]",
1162                            hdr->lc_name, ctr.lc_count, hdr->lc_units);
1163
1164                 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) &&
1165                     (ctr.lc_count > 0)) {
1166                         seq_printf(p, " %lld %lld %lld",
1167                                    ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1168                         if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1169                                 seq_printf(p, " %lld", ctr.lc_sumsquare);
1170                 }
1171                 seq_putc(p, '\n');
1172         }
1173
1174         return 0;
1175 }
1176
1177 static const struct seq_operations lprocfs_stats_seq_sops = {
1178         .start  = lprocfs_stats_seq_start,
1179         .stop   = lprocfs_stats_seq_stop,
1180         .next   = lprocfs_stats_seq_next,
1181         .show   = lprocfs_stats_seq_show,
1182 };
1183
1184 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1185 {
1186         struct seq_file *seq;
1187         int rc;
1188
1189         rc = seq_open(file, &lprocfs_stats_seq_sops);
1190         if (rc)
1191                 return rc;
1192
1193         seq = file->private_data;
1194         seq->private = inode->i_private;
1195
1196         return 0;
1197 }
1198
1199 struct file_operations lprocfs_stats_seq_fops = {
1200         .owner   = THIS_MODULE,
1201         .open    = lprocfs_stats_seq_open,
1202         .read    = seq_read,
1203         .write   = lprocfs_stats_seq_write,
1204         .llseek  = seq_lseek,
1205         .release = lprocfs_seq_release,
1206 };
1207
1208 int ldebugfs_register_stats(struct dentry *parent, const char *name,
1209                            struct lprocfs_stats *stats)
1210 {
1211         struct dentry *entry;
1212
1213         LASSERT(!IS_ERR_OR_NULL(parent));
1214
1215         entry = debugfs_create_file(name, 0644, parent, stats,
1216                                     &lprocfs_stats_seq_fops);
1217         if (IS_ERR_OR_NULL(entry))
1218                 return entry ? PTR_ERR(entry) : -ENOMEM;
1219
1220         return 0;
1221 }
1222 EXPORT_SYMBOL(ldebugfs_register_stats);
1223
1224 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1225                           unsigned conf, const char *name, const char *units)
1226 {
1227         struct lprocfs_counter_header   *header;
1228         struct lprocfs_counter          *percpu_cntr;
1229         unsigned long                   flags = 0;
1230         unsigned int                    i;
1231         unsigned int                    num_cpu;
1232
1233         LASSERT(stats != NULL);
1234
1235         header = &stats->ls_cnt_header[index];
1236         LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1237                  index, name, units);
1238
1239         header->lc_config = conf;
1240         header->lc_name   = name;
1241         header->lc_units  = units;
1242
1243         num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1244         for (i = 0; i < num_cpu; ++i) {
1245                 if (stats->ls_percpu[i] == NULL)
1246                         continue;
1247                 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1248                 percpu_cntr->lc_count           = 0;
1249                 percpu_cntr->lc_min             = LC_MIN_INIT;
1250                 percpu_cntr->lc_max             = 0;
1251                 percpu_cntr->lc_sumsquare       = 0;
1252                 percpu_cntr->lc_sum             = 0;
1253                 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1254                         percpu_cntr->lc_sum_irq = 0;
1255         }
1256         lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1257 }
1258 EXPORT_SYMBOL(lprocfs_counter_init);
1259
1260 int lprocfs_exp_cleanup(struct obd_export *exp)
1261 {
1262         return 0;
1263 }
1264 EXPORT_SYMBOL(lprocfs_exp_cleanup);
1265
1266 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1267                           struct lprocfs_counter_header *header,
1268                           enum lprocfs_stats_flags flags,
1269                           enum lprocfs_fields_flags field)
1270 {
1271         __s64 ret = 0;
1272
1273         if (lc == NULL || header == NULL)
1274                 return 0;
1275
1276         switch (field) {
1277         case LPROCFS_FIELDS_FLAGS_CONFIG:
1278                 ret = header->lc_config;
1279                 break;
1280         case LPROCFS_FIELDS_FLAGS_SUM:
1281                 ret = lc->lc_sum;
1282                 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1283                         ret += lc->lc_sum_irq;
1284                 break;
1285         case LPROCFS_FIELDS_FLAGS_MIN:
1286                 ret = lc->lc_min;
1287                 break;
1288         case LPROCFS_FIELDS_FLAGS_MAX:
1289                 ret = lc->lc_max;
1290                 break;
1291         case LPROCFS_FIELDS_FLAGS_AVG:
1292                 ret = (lc->lc_max - lc->lc_min) / 2;
1293                 break;
1294         case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1295                 ret = lc->lc_sumsquare;
1296                 break;
1297         case LPROCFS_FIELDS_FLAGS_COUNT:
1298                 ret = lc->lc_count;
1299                 break;
1300         default:
1301                 break;
1302         }
1303
1304         return 0;
1305 }
1306 EXPORT_SYMBOL(lprocfs_read_helper);
1307
1308 int lprocfs_write_helper(const char __user *buffer, unsigned long count,
1309                          int *val)
1310 {
1311         return lprocfs_write_frac_helper(buffer, count, val, 1);
1312 }
1313 EXPORT_SYMBOL(lprocfs_write_helper);
1314
1315 int lprocfs_write_u64_helper(const char __user *buffer, unsigned long count,
1316                              __u64 *val)
1317 {
1318         return lprocfs_write_frac_u64_helper(buffer, count, val, 1);
1319 }
1320 EXPORT_SYMBOL(lprocfs_write_u64_helper);
1321
1322 int lprocfs_write_frac_u64_helper(const char *buffer, unsigned long count,
1323                               __u64 *val, int mult)
1324 {
1325         char kernbuf[22], *end, *pbuf;
1326         __u64 whole, frac = 0, units;
1327         unsigned frac_d = 1;
1328         int sign = 1;
1329
1330         if (count > (sizeof(kernbuf) - 1))
1331                 return -EINVAL;
1332
1333         if (copy_from_user(kernbuf, buffer, count))
1334                 return -EFAULT;
1335
1336         kernbuf[count] = '\0';
1337         pbuf = kernbuf;
1338         if (*pbuf == '-') {
1339                 sign = -1;
1340                 pbuf++;
1341         }
1342
1343         whole = simple_strtoull(pbuf, &end, 10);
1344         if (pbuf == end)
1345                 return -EINVAL;
1346
1347         if (*end == '.') {
1348                 int i;
1349
1350                 pbuf = end + 1;
1351
1352                 /* need to limit frac_d to a __u32 */
1353                 if (strlen(pbuf) > 10)
1354                         pbuf[10] = '\0';
1355
1356                 frac = simple_strtoull(pbuf, &end, 10);
1357                 /* count decimal places */
1358                 for (i = 0; i < (end - pbuf); i++)
1359                         frac_d *= 10;
1360         }
1361
1362         units = 1;
1363         switch (tolower(*end)) {
1364         case 'p':
1365                 units <<= 10;
1366         case 't':
1367                 units <<= 10;
1368         case 'g':
1369                 units <<= 10;
1370         case 'm':
1371                 units <<= 10;
1372         case 'k':
1373                 units <<= 10;
1374         }
1375         /* Specified units override the multiplier */
1376         if (units > 1)
1377                 mult = units;
1378
1379         frac *= mult;
1380         do_div(frac, frac_d);
1381         *val = sign * (whole * mult + frac);
1382         return 0;
1383 }
1384 EXPORT_SYMBOL(lprocfs_write_frac_u64_helper);
1385
1386 static char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1387 {
1388         size_t l2;
1389
1390         l2 = strlen(s2);
1391         if (!l2)
1392                 return (char *)s1;
1393         while (len >= l2) {
1394                 len--;
1395                 if (!memcmp(s1, s2, l2))
1396                         return (char *)s1;
1397                 s1++;
1398         }
1399         return NULL;
1400 }
1401
1402 /**
1403  * Find the string \a name in the input \a buffer, and return a pointer to the
1404  * value immediately following \a name, reducing \a count appropriately.
1405  * If \a name is not found the original \a buffer is returned.
1406  */
1407 char *lprocfs_find_named_value(const char *buffer, const char *name,
1408                                size_t *count)
1409 {
1410         char *val;
1411         size_t buflen = *count;
1412
1413         /* there is no strnstr() in rhel5 and ubuntu kernels */
1414         val = lprocfs_strnstr(buffer, name, buflen);
1415         if (val == NULL)
1416                 return (char *)buffer;
1417
1418         val += strlen(name);                         /* skip prefix */
1419         while (val < buffer + buflen && isspace(*val)) /* skip separator */
1420                 val++;
1421
1422         *count = 0;
1423         while (val < buffer + buflen && isalnum(*val)) {
1424                 ++*count;
1425                 ++val;
1426         }
1427
1428         return val - *count;
1429 }
1430 EXPORT_SYMBOL(lprocfs_find_named_value);
1431
1432 int ldebugfs_seq_create(struct dentry *parent,
1433                        const char *name,
1434                        umode_t mode,
1435                        const struct file_operations *seq_fops,
1436                        void *data)
1437 {
1438         struct dentry *entry;
1439
1440         /* Disallow secretly (un)writable entries. */
1441         LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
1442
1443         entry = debugfs_create_file(name, mode, parent, data, seq_fops);
1444         if (IS_ERR_OR_NULL(entry))
1445                 return entry ? PTR_ERR(entry) : -ENOMEM;
1446
1447         return 0;
1448 }
1449 EXPORT_SYMBOL(ldebugfs_seq_create);
1450
1451 int ldebugfs_obd_seq_create(struct obd_device *dev,
1452                             const char *name,
1453                             umode_t mode,
1454                             const struct file_operations *seq_fops,
1455                             void *data)
1456 {
1457         return ldebugfs_seq_create(dev->obd_debugfs_entry, name,
1458                                    mode, seq_fops, data);
1459 }
1460 EXPORT_SYMBOL(ldebugfs_obd_seq_create);
1461
1462 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
1463 {
1464         if (value >= OBD_HIST_MAX)
1465                 value = OBD_HIST_MAX - 1;
1466
1467         spin_lock(&oh->oh_lock);
1468         oh->oh_buckets[value]++;
1469         spin_unlock(&oh->oh_lock);
1470 }
1471 EXPORT_SYMBOL(lprocfs_oh_tally);
1472
1473 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
1474 {
1475         unsigned int val;
1476
1477         for (val = 0; ((1 << val) < value) && (val <= OBD_HIST_MAX); val++)
1478                 ;
1479
1480         lprocfs_oh_tally(oh, val);
1481 }
1482 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
1483
1484 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
1485 {
1486         unsigned long ret = 0;
1487         int i;
1488
1489         for (i = 0; i < OBD_HIST_MAX; i++)
1490                 ret +=  oh->oh_buckets[i];
1491         return ret;
1492 }
1493 EXPORT_SYMBOL(lprocfs_oh_sum);
1494
1495 void lprocfs_oh_clear(struct obd_histogram *oh)
1496 {
1497         spin_lock(&oh->oh_lock);
1498         memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
1499         spin_unlock(&oh->oh_lock);
1500 }
1501 EXPORT_SYMBOL(lprocfs_oh_clear);
1502
1503 static ssize_t lustre_attr_show(struct kobject *kobj,
1504                                 struct attribute *attr, char *buf)
1505 {
1506         struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1507
1508         return a->show ? a->show(kobj, attr, buf) : 0;
1509 }
1510
1511 static ssize_t lustre_attr_store(struct kobject *kobj, struct attribute *attr,
1512                                  const char *buf, size_t len)
1513 {
1514         struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1515
1516         return a->store ? a->store(kobj, attr, buf, len) : len;
1517 }
1518
1519 const struct sysfs_ops lustre_sysfs_ops = {
1520         .show  = lustre_attr_show,
1521         .store = lustre_attr_store,
1522 };
1523 EXPORT_SYMBOL_GPL(lustre_sysfs_ops);