4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/obdclass/lprocfs_status.c
34 * Author: Hariharan Thantry <thantry@users.sourceforge.net>
37 #define DEBUG_SUBSYSTEM S_CLASS
39 #include <obd_class.h>
40 #include <lprocfs_status.h>
44 static int lprocfs_no_percpu_stats = 0;
45 module_param(lprocfs_no_percpu_stats, int, 0644);
46 MODULE_PARM_DESC(lprocfs_no_percpu_stats, "Do not alloc percpu data for lprocfs stats");
48 #define MAX_STRING_SIZE 128
50 int lprocfs_single_release(struct inode *inode, struct file *file)
52 return single_release(inode, file);
54 EXPORT_SYMBOL(lprocfs_single_release);
56 int lprocfs_seq_release(struct inode *inode, struct file *file)
58 return seq_release(inode, file);
60 EXPORT_SYMBOL(lprocfs_seq_release);
62 struct dentry *ldebugfs_add_simple(struct dentry *root,
63 char *name, void *data,
64 const struct file_operations *fops)
69 if (!root || !name || !fops)
70 return ERR_PTR(-EINVAL);
76 entry = debugfs_create_file(name, mode, root, data, fops);
77 if (IS_ERR_OR_NULL(entry)) {
78 CERROR("LprocFS: No memory to create <debugfs> entry %s", name);
79 return entry ?: ERR_PTR(-ENOMEM);
83 EXPORT_SYMBOL(ldebugfs_add_simple);
85 struct proc_dir_entry *
86 lprocfs_add_simple(struct proc_dir_entry *root, char *name,
87 void *data, const struct file_operations *fops)
89 struct proc_dir_entry *proc;
92 if (!root || !name || !fops)
93 return ERR_PTR(-EINVAL);
99 proc = proc_create_data(name, mode, root, fops, data);
101 CERROR("LprocFS: No memory to create /proc entry %s\n",
103 return ERR_PTR(-ENOMEM);
107 EXPORT_SYMBOL(lprocfs_add_simple);
109 struct proc_dir_entry *lprocfs_add_symlink(const char *name,
110 struct proc_dir_entry *parent,
111 const char *format, ...)
113 struct proc_dir_entry *entry;
117 if (!parent || !format)
120 OBD_ALLOC_WAIT(dest, MAX_STRING_SIZE + 1);
124 va_start(ap, format);
125 vsnprintf(dest, MAX_STRING_SIZE, format, ap);
128 entry = proc_symlink(name, parent, dest);
130 CERROR("LprocFS: Could not create symbolic link from "
131 "%s to %s\n", name, dest);
133 OBD_FREE(dest, MAX_STRING_SIZE + 1);
136 EXPORT_SYMBOL(lprocfs_add_symlink);
138 static const struct file_operations lprocfs_generic_fops = { };
140 int ldebugfs_add_vars(struct dentry *parent, struct lprocfs_vars *list,
143 if (IS_ERR_OR_NULL(parent) || IS_ERR_OR_NULL(list))
147 struct dentry *entry;
150 if (list->proc_mode != 0000) {
151 mode = list->proc_mode;
152 } else if (list->fops) {
153 if (list->fops->read)
155 if (list->fops->write)
158 entry = debugfs_create_file(list->name, mode, parent,
160 list->fops ? : &lprocfs_generic_fops);
161 if (IS_ERR_OR_NULL(entry))
162 return entry ? PTR_ERR(entry) : -ENOMEM;
167 EXPORT_SYMBOL_GPL(ldebugfs_add_vars);
172 * \param root [in] The parent proc entry on which new entry will be added.
173 * \param list [in] Array of proc entries to be added.
174 * \param data [in] The argument to be passed when entries read/write routines
175 * are called through /proc file.
177 * \retval 0 on success
181 lprocfs_add_vars(struct proc_dir_entry *root, struct lprocfs_vars *list,
188 struct proc_dir_entry *proc;
191 if (list->proc_mode != 0000) {
192 mode = list->proc_mode;
193 } else if (list->fops) {
194 if (list->fops->read)
196 if (list->fops->write)
199 proc = proc_create_data(list->name, mode, root,
200 list->fops ?: &lprocfs_generic_fops,
208 EXPORT_SYMBOL(lprocfs_add_vars);
210 void ldebugfs_remove(struct dentry **entryp)
212 debugfs_remove_recursive(*entryp);
215 EXPORT_SYMBOL_GPL(ldebugfs_remove);
217 #ifndef HAVE_REMOVE_PROC_SUBTREE
218 /* for b=10866, global variable */
219 DECLARE_RWSEM(_lprocfs_lock);
220 EXPORT_SYMBOL(_lprocfs_lock);
222 static void lprocfs_remove_nolock(struct proc_dir_entry **proot)
224 struct proc_dir_entry *root = *proot;
225 struct proc_dir_entry *temp = root;
226 struct proc_dir_entry *rm_entry;
227 struct proc_dir_entry *parent;
230 if (!root || IS_ERR(root))
233 parent = root->parent;
234 LASSERT(parent != NULL);
244 * Memory corruption once caused this to fail, and
245 * without this LASSERT we would loop here forever.
247 LASSERTF(strlen(rm_entry->name) == rm_entry->namelen,
248 "0x%p %s/%s len %d\n", rm_entry, temp->name,
249 rm_entry->name, (int)strlen(rm_entry->name));
251 remove_proc_entry(rm_entry->name, temp);
257 int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
259 struct proc_dir_entry *t = NULL;
260 struct proc_dir_entry **p;
263 LASSERT(parent != NULL);
266 down_write(&_lprocfs_lock);
267 /* lookup target name */
268 for (p = &parent->subdir; *p; p = &(*p)->next) {
269 if ((*p)->namelen != len)
271 if (memcmp(name, (*p)->name, len))
278 /* verify it's empty: do not count "num_refs" */
279 for (p = &t->subdir; *p; p = &(*p)->next) {
280 if ((*p)->namelen != strlen("num_refs")) {
284 if (memcmp("num_refs", (*p)->name,
285 strlen("num_refs"))) {
293 lprocfs_remove_nolock(&t);
295 up_write(&_lprocfs_lock);
298 #endif /* !HAVE_REMOVE_PROC_SUBTREE */
300 #ifndef HAVE_PROC_REMOVE
301 void proc_remove(struct proc_dir_entry *de)
303 #ifndef HAVE_REMOVE_PROC_SUBTREE
304 down_write(&_lprocfs_lock); /* search vs remove race */
305 lprocfs_remove_nolock(&de);
306 up_write(&_lprocfs_lock);
309 remove_proc_subtree(de->name, de->parent);
314 void lprocfs_remove(struct proc_dir_entry **rooth)
319 EXPORT_SYMBOL(lprocfs_remove);
321 void lprocfs_remove_proc_entry(const char *name, struct proc_dir_entry *parent)
323 LASSERT(parent != NULL);
324 remove_proc_entry(name, parent);
326 EXPORT_SYMBOL(lprocfs_remove_proc_entry);
328 struct dentry *ldebugfs_register(const char *name, struct dentry *parent,
329 struct lprocfs_vars *list, void *data)
331 struct dentry *entry;
333 entry = debugfs_create_dir(name, parent);
334 if (IS_ERR_OR_NULL(entry)) {
335 entry = entry ?: ERR_PTR(-ENOMEM);
339 if (!IS_ERR_OR_NULL(list)) {
342 rc = ldebugfs_add_vars(entry, list, data);
344 debugfs_remove(entry);
351 EXPORT_SYMBOL_GPL(ldebugfs_register);
353 struct proc_dir_entry *
354 lprocfs_register(const char *name, struct proc_dir_entry *parent,
355 struct lprocfs_vars *list, void *data)
357 struct proc_dir_entry *newchild;
359 newchild = proc_mkdir(name, parent);
361 return ERR_PTR(-ENOMEM);
364 int rc = lprocfs_add_vars(newchild, list, data);
366 lprocfs_remove(&newchild);
372 EXPORT_SYMBOL(lprocfs_register);
374 /* Generic callbacks */
375 int lprocfs_uuid_seq_show(struct seq_file *m, void *data)
377 struct obd_device *obd = data;
379 LASSERT(obd != NULL);
380 seq_printf(m, "%s\n", obd->obd_uuid.uuid);
383 EXPORT_SYMBOL(lprocfs_uuid_seq_show);
385 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
388 struct obd_device *obd = container_of(kobj, struct obd_device,
391 return sprintf(buf, "%s\n", obd->obd_uuid.uuid);
393 LUSTRE_RO_ATTR(uuid);
395 static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
398 struct obd_device *obd = container_of(kobj, struct obd_device,
400 struct obd_statfs osfs;
403 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
404 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
407 return sprintf(buf, "%u\n", osfs.os_bsize);
411 LUSTRE_RO_ATTR(blocksize);
413 static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
416 struct obd_device *obd = container_of(kobj, struct obd_device,
418 struct obd_statfs osfs;
421 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
422 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
425 u32 blk_size = osfs.os_bsize >> 10;
426 u64 result = osfs.os_blocks;
428 while (blk_size >>= 1)
431 return sprintf(buf, "%llu\n", result);
436 LUSTRE_RO_ATTR(kbytestotal);
438 static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
441 struct obd_device *obd = container_of(kobj, struct obd_device,
443 struct obd_statfs osfs;
446 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
447 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
450 u32 blk_size = osfs.os_bsize >> 10;
451 u64 result = osfs.os_bfree;
453 while (blk_size >>= 1)
456 return sprintf(buf, "%llu\n", result);
461 LUSTRE_RO_ATTR(kbytesfree);
463 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
466 struct obd_device *obd = container_of(kobj, struct obd_device,
468 struct obd_statfs osfs;
471 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
472 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
475 u32 blk_size = osfs.os_bsize >> 10;
476 u64 result = osfs.os_bavail;
478 while (blk_size >>= 1)
481 return sprintf(buf, "%llu\n", result);
486 LUSTRE_RO_ATTR(kbytesavail);
488 static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
491 struct obd_device *obd = container_of(kobj, struct obd_device,
493 struct obd_statfs osfs;
496 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
497 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
500 return sprintf(buf, "%llu\n", osfs.os_files);
504 LUSTRE_RO_ATTR(filestotal);
506 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
509 struct obd_device *obd = container_of(kobj, struct obd_device,
511 struct obd_statfs osfs;
514 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
515 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
518 return sprintf(buf, "%llu\n", osfs.os_ffree);
522 LUSTRE_RO_ATTR(filesfree);
524 ssize_t conn_uuid_show(struct kobject *kobj, struct attribute *attr, char *buf)
526 struct obd_device *obd = container_of(kobj, struct obd_device,
528 struct ptlrpc_connection *conn;
531 LPROCFS_CLIMP_CHECK(obd);
532 conn = obd->u.cli.cl_import->imp_connection;
533 if (conn && obd->u.cli.cl_import)
534 count = sprintf(buf, "%s\n", conn->c_remote_uuid.uuid);
536 count = sprintf(buf, "%s\n", "<none>");
538 LPROCFS_CLIMP_EXIT(obd);
541 EXPORT_SYMBOL(conn_uuid_show);
543 int lprocfs_server_uuid_seq_show(struct seq_file *m, void *data)
545 struct obd_device *obd = data;
546 struct obd_import *imp;
547 char *imp_state_name = NULL;
550 LASSERT(obd != NULL);
551 LPROCFS_CLIMP_CHECK(obd);
552 imp = obd->u.cli.cl_import;
553 imp_state_name = ptlrpc_import_state_name(imp->imp_state);
554 seq_printf(m, "%s\t%s%s\n", obd2cli_tgt(obd), imp_state_name,
555 imp->imp_deactive ? "\tDEACTIVATED" : "");
557 LPROCFS_CLIMP_EXIT(obd);
560 EXPORT_SYMBOL(lprocfs_server_uuid_seq_show);
562 /** add up per-cpu counters */
565 * Lock statistics structure for access, possibly only on this CPU.
567 * The statistics struct may be allocated with per-CPU structures for
568 * efficient concurrent update (usually only on server-wide stats), or
569 * as a single global struct (e.g. for per-client or per-job statistics),
570 * so the required locking depends on the type of structure allocated.
572 * For per-CPU statistics, pin the thread to the current cpuid so that
573 * will only access the statistics for that CPU. If the stats structure
574 * for the current CPU has not been allocated (or previously freed),
575 * allocate it now. The per-CPU statistics do not need locking since
576 * the thread is pinned to the CPU during update.
578 * For global statistics, lock the stats structure to prevent concurrent update.
580 * \param[in] stats statistics structure to lock
581 * \param[in] opc type of operation:
582 * LPROCFS_GET_SMP_ID: "lock" and return current CPU index
583 * for incrementing statistics for that CPU
584 * LPROCFS_GET_NUM_CPU: "lock" and return number of used
585 * CPU indices to iterate over all indices
586 * \param[out] flags CPU interrupt saved state for IRQ-safe locking
588 * \retval cpuid of current thread or number of allocated structs
589 * \retval negative on error (only for opc LPROCFS_GET_SMP_ID + per-CPU stats)
591 int lprocfs_stats_lock(struct lprocfs_stats *stats,
592 enum lprocfs_stats_lock_ops opc,
593 unsigned long *flags)
595 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
596 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
597 spin_lock_irqsave(&stats->ls_lock, *flags);
599 spin_lock(&stats->ls_lock);
600 return opc == LPROCFS_GET_NUM_CPU ? 1 : 0;
604 case LPROCFS_GET_SMP_ID: {
605 unsigned int cpuid = get_cpu();
607 if (unlikely(!stats->ls_percpu[cpuid])) {
608 int rc = lprocfs_stats_alloc_one(stats, cpuid);
617 case LPROCFS_GET_NUM_CPU:
618 return stats->ls_biggest_alloc_num;
625 * Unlock statistics structure after access.
627 * Unlock the lock acquired via lprocfs_stats_lock() for global statistics,
628 * or unpin this thread from the current cpuid for per-CPU statistics.
630 * This function must be called using the same arguments as used when calling
631 * lprocfs_stats_lock() so that the correct operation can be performed.
633 * \param[in] stats statistics structure to unlock
634 * \param[in] opc type of operation (current cpuid or number of structs)
635 * \param[in] flags CPU interrupt saved state for IRQ-safe locking
637 void lprocfs_stats_unlock(struct lprocfs_stats *stats,
638 enum lprocfs_stats_lock_ops opc,
639 unsigned long *flags)
641 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
642 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
643 spin_unlock_irqrestore(&stats->ls_lock, *flags);
645 spin_unlock(&stats->ls_lock);
646 } else if (opc == LPROCFS_GET_SMP_ID) {
651 /** add up per-cpu counters */
652 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
653 struct lprocfs_counter *cnt)
655 unsigned int num_entry;
656 struct lprocfs_counter *percpu_cntr;
658 unsigned long flags = 0;
660 memset(cnt, 0, sizeof(*cnt));
663 /* set count to 1 to avoid divide-by-zero errs in callers */
668 cnt->lc_min = LC_MIN_INIT;
670 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
672 for (i = 0; i < num_entry; i++) {
673 if (!stats->ls_percpu[i])
675 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
677 cnt->lc_count += percpu_cntr->lc_count;
678 cnt->lc_sum += percpu_cntr->lc_sum;
679 if (percpu_cntr->lc_min < cnt->lc_min)
680 cnt->lc_min = percpu_cntr->lc_min;
681 if (percpu_cntr->lc_max > cnt->lc_max)
682 cnt->lc_max = percpu_cntr->lc_max;
683 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
686 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
689 static void obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
693 if (imp->imp_obd->obd_no_recov) {
694 seq_printf(m, "no_recov");
698 flag2str(imp, invalid);
699 flag2str(imp, deactive);
700 flag2str(imp, replayable);
701 flag2str(imp, delayed_recovery);
702 flag2str(imp, vbr_failed);
703 flag2str(imp, pingable);
704 flag2str(imp, resend_replay);
705 flag2str(imp, no_pinger_recover);
706 flag2str(imp, connect_tried);
709 static const char *obd_connect_names[] = {
728 "remote_client_by_force",
737 "mds_mds_connection",
740 "alt_checksum_algorithm",
776 "file_secctx", /* 0x01 */
777 "lockaheadv2", /* 0x02 */
778 "dir_migrate", /* 0x04 */
779 "sum_statfs", /* 0x08 */
780 "overstriping", /* 0x10 */
783 "lock_convert", /* 0x80 */
784 "archive_id_array", /* 0x100 */
785 "increasing_xid", /* 0x200 */
786 "selinux_policy", /* 0x400 */
789 "unknown", /* 0x2000 */
790 "async_discard", /* 0x4000 */
791 "client_encryption", /* 0x8000 */
792 "fidmap", /* 0x10000 */
793 "getattr_pfid", /* 0x20000 */
797 void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, __u64 flags2,
804 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
806 seq_printf(m, "%s%s",
807 first ? "" : sep, obd_connect_names[i]);
812 if (flags & ~(mask - 1)) {
813 seq_printf(m, "%sunknown_%#llx",
814 first ? "" : sep, flags & ~(mask - 1));
818 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
821 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
823 seq_printf(m, "%s%s",
824 first ? "" : sep, obd_connect_names[i]);
829 if (flags2 & ~(mask - 1)) {
830 seq_printf(m, "%sunknown2_%#llx",
831 first ? "" : sep, flags2 & ~(mask - 1));
835 EXPORT_SYMBOL(obd_connect_seq_flags2str);
837 int obd_connect_flags2str(char *page, int count, __u64 flags, __u64 flags2,
843 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
845 ret += snprintf(page + ret, count - ret, "%s%s",
846 ret ? sep : "", obd_connect_names[i]);
849 if (flags & ~(mask - 1))
850 ret += snprintf(page + ret, count - ret,
852 ret ? sep : "", flags & ~(mask - 1));
854 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
857 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
859 ret += snprintf(page + ret, count - ret, "%s%s",
860 ret ? sep : "", obd_connect_names[i]);
863 if (flags2 & ~(mask - 1))
864 ret += snprintf(page + ret, count - ret,
866 ret ? sep : "", flags2 & ~(mask - 1));
870 EXPORT_SYMBOL(obd_connect_flags2str);
873 obd_connect_data_seqprint(struct seq_file *m, struct obd_connect_data *ocd)
877 LASSERT(ocd != NULL);
878 flags = ocd->ocd_connect_flags;
880 seq_printf(m, " connect_data:\n"
883 ocd->ocd_connect_flags,
885 if (flags & OBD_CONNECT_VERSION)
886 seq_printf(m, " target_version: %u.%u.%u.%u\n",
887 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
888 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
889 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
890 OBD_OCD_VERSION_FIX(ocd->ocd_version));
891 if (flags & OBD_CONNECT_MDS)
892 seq_printf(m, " mdt_index: %d\n", ocd->ocd_group);
893 if (flags & OBD_CONNECT_GRANT)
894 seq_printf(m, " initial_grant: %d\n", ocd->ocd_grant);
895 if (flags & OBD_CONNECT_INDEX)
896 seq_printf(m, " target_index: %u\n", ocd->ocd_index);
897 if (flags & OBD_CONNECT_BRW_SIZE)
898 seq_printf(m, " max_brw_size: %d\n", ocd->ocd_brw_size);
899 if (flags & OBD_CONNECT_IBITS)
900 seq_printf(m, " ibits_known: %#llx\n",
901 ocd->ocd_ibits_known);
902 if (flags & OBD_CONNECT_GRANT_PARAM)
903 seq_printf(m, " grant_block_size: %d\n"
904 " grant_inode_size: %d\n"
905 " grant_max_extent_size: %d\n"
906 " grant_extent_tax: %d\n",
907 1 << ocd->ocd_grant_blkbits,
908 1 << ocd->ocd_grant_inobits,
909 ocd->ocd_grant_max_blks << ocd->ocd_grant_blkbits,
910 ocd->ocd_grant_tax_kb << 10);
911 if (flags & OBD_CONNECT_TRANSNO)
912 seq_printf(m, " first_transno: %#llx\n",
914 if (flags & OBD_CONNECT_CKSUM)
915 seq_printf(m, " cksum_types: %#x\n",
916 ocd->ocd_cksum_types);
917 if (flags & OBD_CONNECT_MAX_EASIZE)
918 seq_printf(m, " max_easize: %d\n", ocd->ocd_max_easize);
919 if (flags & OBD_CONNECT_MAXBYTES)
920 seq_printf(m, " max_object_bytes: %llu\n",
922 if (flags & OBD_CONNECT_MULTIMODRPCS)
923 seq_printf(m, " max_mod_rpcs: %hu\n",
924 ocd->ocd_maxmodrpcs);
927 int lprocfs_import_seq_show(struct seq_file *m, void *data)
929 char nidstr[LNET_NIDSTR_SIZE];
930 struct lprocfs_counter ret;
931 struct lprocfs_counter_header *header;
932 struct obd_device *obd = (struct obd_device *)data;
933 struct obd_import *imp;
934 struct obd_import_conn *conn;
935 struct obd_connect_data *ocd;
940 LASSERT(obd != NULL);
941 LPROCFS_CLIMP_CHECK(obd);
942 imp = obd->u.cli.cl_import;
943 ocd = &imp->imp_connect_data;
945 seq_printf(m, "import:\n"
949 " connect_flags: [ ",
952 ptlrpc_import_state_name(imp->imp_state));
953 obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
954 imp->imp_connect_data.ocd_connect_flags2,
956 seq_printf(m, " ]\n");
957 obd_connect_data_seqprint(m, ocd);
958 seq_printf(m, " import_flags: [ ");
959 obd_import_flags2str(imp, m);
963 " failover_nids: [ ");
964 spin_lock(&imp->imp_lock);
966 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
967 libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
968 nidstr, sizeof(nidstr));
969 seq_printf(m, "%s%s", j ? ", " : "", nidstr);
972 if (imp->imp_connection)
973 libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
974 nidstr, sizeof(nidstr));
976 strncpy(nidstr, "<none>", sizeof(nidstr));
978 " current_connection: %s\n"
979 " connection_attempts: %u\n"
981 " in-progress_invalidations: %u\n"
986 atomic_read(&imp->imp_inval_count),
987 ktime_get_real_seconds() - imp->imp_last_reply_time);
988 spin_unlock(&imp->imp_lock);
990 if (!obd->obd_svc_stats)
993 header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
994 lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
995 if (ret.lc_count != 0) {
996 /* first argument to do_div MUST be __u64 */
997 __u64 sum = ret.lc_sum;
998 do_div(sum, ret.lc_count);
1002 seq_printf(m, " rpcs:\n"
1004 " unregistering: %u\n"
1006 " avg_waittime: %llu %s\n",
1007 atomic_read(&imp->imp_inflight),
1008 atomic_read(&imp->imp_unregistering),
1009 atomic_read(&imp->imp_timeouts),
1010 ret.lc_sum, header->lc_units);
1013 for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
1014 if (imp->imp_at.iat_portal[j] == 0)
1016 k = max_t(unsigned int, k,
1017 at_get(&imp->imp_at.iat_service_estimate[j]));
1019 seq_printf(m, " service_estimates:\n"
1020 " services: %u sec\n"
1021 " network: %u sec\n",
1023 at_get(&imp->imp_at.iat_net_latency));
1025 seq_printf(m, " transactions:\n"
1026 " last_replay: %llu\n"
1027 " peer_committed: %llu\n"
1028 " last_checked: %llu\n",
1029 imp->imp_last_replay_transno,
1030 imp->imp_peer_committed_transno,
1031 imp->imp_last_transno_checked);
1033 /* avg data rates */
1034 for (rw = 0; rw <= 1; rw++) {
1035 lprocfs_stats_collect(obd->obd_svc_stats,
1036 PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
1038 if (ret.lc_sum > 0 && ret.lc_count > 0) {
1039 /* first argument to do_div MUST be __u64 */
1040 __u64 sum = ret.lc_sum;
1041 do_div(sum, ret.lc_count);
1043 seq_printf(m, " %s_data_averages:\n"
1044 " bytes_per_rpc: %llu\n",
1045 rw ? "write" : "read",
1048 k = (int)ret.lc_sum;
1049 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
1050 header = &obd->obd_svc_stats->ls_cnt_header[j];
1051 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
1052 if (ret.lc_sum > 0 && ret.lc_count != 0) {
1053 /* first argument to do_div MUST be __u64 */
1054 __u64 sum = ret.lc_sum;
1055 do_div(sum, ret.lc_count);
1057 seq_printf(m, " %s_per_rpc: %llu\n",
1058 header->lc_units, ret.lc_sum);
1059 j = (int)ret.lc_sum;
1061 seq_printf(m, " MB_per_sec: %u.%.02u\n",
1062 k / j, (100 * k / j) % 100);
1067 LPROCFS_CLIMP_EXIT(obd);
1070 EXPORT_SYMBOL(lprocfs_import_seq_show);
1072 int lprocfs_state_seq_show(struct seq_file *m, void *data)
1074 struct obd_device *obd = (struct obd_device *)data;
1075 struct obd_import *imp;
1078 LASSERT(obd != NULL);
1079 LPROCFS_CLIMP_CHECK(obd);
1080 imp = obd->u.cli.cl_import;
1082 seq_printf(m, "current_state: %s\n",
1083 ptlrpc_import_state_name(imp->imp_state));
1084 seq_printf(m, "state_history:\n");
1085 k = imp->imp_state_hist_idx;
1086 for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
1087 struct import_state_hist *ish =
1088 &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
1089 if (ish->ish_state == 0)
1091 seq_printf(m, " - [ %lld, %s ]\n", (s64)ish->ish_time,
1092 ptlrpc_import_state_name(ish->ish_state));
1095 LPROCFS_CLIMP_EXIT(obd);
1098 EXPORT_SYMBOL(lprocfs_state_seq_show);
1100 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
1103 for (i = 0; i < AT_BINS; i++)
1104 seq_printf(m, "%3u ", at->at_hist[i]);
1105 seq_printf(m, "\n");
1108 EXPORT_SYMBOL(lprocfs_at_hist_helper);
1110 /* See also ptlrpc_lprocfs_timeouts_show_seq */
1111 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
1113 struct obd_device *obd = (struct obd_device *)data;
1114 struct obd_import *imp;
1115 unsigned int cur, worst;
1116 time64_t now, worstt;
1119 LASSERT(obd != NULL);
1120 LPROCFS_CLIMP_CHECK(obd);
1121 imp = obd->u.cli.cl_import;
1123 now = ktime_get_real_seconds();
1125 /* Some network health info for kicks */
1126 seq_printf(m, "%-10s : %lld, %llds ago\n",
1127 "last reply", (s64)imp->imp_last_reply_time,
1128 (s64)(now - imp->imp_last_reply_time));
1130 cur = at_get(&imp->imp_at.iat_net_latency);
1131 worst = imp->imp_at.iat_net_latency.at_worst_ever;
1132 worstt = imp->imp_at.iat_net_latency.at_worst_time;
1133 seq_printf(m, "%-10s : cur %3u worst %3u (at %lld, %llds ago) ",
1134 "network", cur, worst, (s64)worstt, (s64)(now - worstt));
1135 lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
1137 for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1138 if (imp->imp_at.iat_portal[i] == 0)
1140 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
1141 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
1142 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
1143 seq_printf(m, "portal %-2d : cur %3u worst %3u (at %lld, %llds ago) ",
1144 imp->imp_at.iat_portal[i], cur, worst, (s64)worstt,
1145 (s64)(now - worstt));
1146 lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
1149 LPROCFS_CLIMP_EXIT(obd);
1152 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
1154 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
1156 struct obd_device *obd = data;
1160 LPROCFS_CLIMP_CHECK(obd);
1161 flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
1162 flags2 = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags2;
1163 seq_printf(m, "flags=%#llx\n", flags);
1164 seq_printf(m, "flags2=%#llx\n", flags2);
1165 obd_connect_seq_flags2str(m, flags, flags2, "\n");
1166 seq_printf(m, "\n");
1167 LPROCFS_CLIMP_EXIT(obd);
1170 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
1172 static const struct attribute *obd_def_uuid_attrs[] = {
1173 &lustre_attr_uuid.attr,
1177 static const struct attribute *obd_def_attrs[] = {
1178 &lustre_attr_blocksize.attr,
1179 &lustre_attr_kbytestotal.attr,
1180 &lustre_attr_kbytesfree.attr,
1181 &lustre_attr_kbytesavail.attr,
1182 &lustre_attr_filestotal.attr,
1183 &lustre_attr_filesfree.attr,
1184 &lustre_attr_uuid.attr,
1188 static void obd_sysfs_release(struct kobject *kobj)
1190 struct obd_device *obd = container_of(kobj, struct obd_device,
1193 complete(&obd->obd_kobj_unregister);
1196 int lprocfs_obd_setup(struct obd_device *obd, bool uuid_only)
1198 struct lprocfs_vars *debugfs_vars = NULL;
1201 if (!obd || obd->obd_magic != OBD_DEVICE_MAGIC)
1204 rc = kobject_set_name(&obd->obd_kset.kobj, "%s", obd->obd_name);
1208 obd->obd_ktype.sysfs_ops = &lustre_sysfs_ops;
1209 obd->obd_ktype.release = obd_sysfs_release;
1211 obd->obd_kset.kobj.parent = obd->obd_type->typ_kobj;
1212 obd->obd_kset.kobj.ktype = &obd->obd_ktype;
1213 init_completion(&obd->obd_kobj_unregister);
1214 rc = kset_register(&obd->obd_kset);
1219 obd->obd_attrs = obd_def_uuid_attrs;
1221 obd->obd_attrs = obd_def_attrs;
1223 rc = sysfs_create_files(&obd->obd_kset.kobj, obd->obd_attrs);
1225 kset_unregister(&obd->obd_kset);
1229 if (!obd->obd_type->typ_procroot)
1230 debugfs_vars = obd->obd_vars;
1231 obd->obd_debugfs_entry = ldebugfs_register(obd->obd_name,
1232 obd->obd_type->typ_debugfs_entry,
1234 if (IS_ERR_OR_NULL(obd->obd_debugfs_entry)) {
1235 rc = obd->obd_debugfs_entry ? PTR_ERR(obd->obd_debugfs_entry)
1237 CERROR("error %d setting up debugfs for %s\n",
1239 obd->obd_debugfs_entry = NULL;
1241 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1242 obd->obd_attrs = NULL;
1243 kset_unregister(&obd->obd_kset);
1247 if (obd->obd_proc_entry || !obd->obd_type->typ_procroot)
1248 GOTO(already_registered, rc);
1250 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1251 obd->obd_type->typ_procroot,
1252 obd->obd_vars, obd);
1253 if (IS_ERR(obd->obd_proc_entry)) {
1254 rc = PTR_ERR(obd->obd_proc_entry);
1255 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1256 obd->obd_proc_entry = NULL;
1258 ldebugfs_remove(&obd->obd_debugfs_entry);
1259 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1260 obd->obd_attrs = NULL;
1261 kset_unregister(&obd->obd_kset);
1267 EXPORT_SYMBOL(lprocfs_obd_setup);
1269 int lprocfs_obd_cleanup(struct obd_device *obd)
1274 if (obd->obd_proc_exports_entry) {
1275 /* Should be no exports left */
1276 lprocfs_remove(&obd->obd_proc_exports_entry);
1277 obd->obd_proc_exports_entry = NULL;
1280 if (obd->obd_proc_entry) {
1281 lprocfs_remove(&obd->obd_proc_entry);
1282 obd->obd_proc_entry = NULL;
1285 if (!IS_ERR_OR_NULL(obd->obd_debugfs_entry))
1286 ldebugfs_remove(&obd->obd_debugfs_entry);
1288 /* obd device never allocated a kset */
1289 if (!obd->obd_kset.kobj.state_initialized)
1292 if (obd->obd_attrs) {
1293 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1294 obd->obd_attrs = NULL;
1297 kset_unregister(&obd->obd_kset);
1298 wait_for_completion(&obd->obd_kobj_unregister);
1301 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1303 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1305 struct lprocfs_counter *cntr;
1306 unsigned int percpusize;
1308 unsigned long flags = 0;
1311 LASSERT(stats->ls_percpu[cpuid] == NULL);
1312 LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1314 percpusize = lprocfs_stats_counter_size(stats);
1315 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1316 if (stats->ls_percpu[cpuid]) {
1318 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1319 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1320 spin_lock_irqsave(&stats->ls_lock, flags);
1322 spin_lock(&stats->ls_lock);
1323 if (stats->ls_biggest_alloc_num <= cpuid)
1324 stats->ls_biggest_alloc_num = cpuid + 1;
1325 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1326 spin_unlock_irqrestore(&stats->ls_lock, flags);
1328 spin_unlock(&stats->ls_lock);
1331 /* initialize the ls_percpu[cpuid] non-zero counter */
1332 for (i = 0; i < stats->ls_num; ++i) {
1333 cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1334 cntr->lc_min = LC_MIN_INIT;
1340 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1341 enum lprocfs_stats_flags flags)
1343 struct lprocfs_stats *stats;
1344 unsigned int num_entry;
1345 unsigned int percpusize = 0;
1351 if (lprocfs_no_percpu_stats != 0)
1352 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1354 if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1357 num_entry = num_possible_cpus();
1359 /* alloc percpu pointers for all possible cpu slots */
1360 LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1364 stats->ls_num = num;
1365 stats->ls_flags = flags;
1366 spin_lock_init(&stats->ls_lock);
1368 /* alloc num of counter headers */
1369 LIBCFS_ALLOC(stats->ls_cnt_header,
1370 stats->ls_num * sizeof(struct lprocfs_counter_header));
1371 if (!stats->ls_cnt_header)
1374 if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1375 /* contains only one set counters */
1376 percpusize = lprocfs_stats_counter_size(stats);
1377 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1378 if (!stats->ls_percpu[0])
1380 stats->ls_biggest_alloc_num = 1;
1381 } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1382 /* alloc all percpu data, currently only obd_memory use this */
1383 for (i = 0; i < num_entry; ++i)
1384 if (lprocfs_stats_alloc_one(stats, i) < 0)
1391 lprocfs_free_stats(&stats);
1394 EXPORT_SYMBOL(lprocfs_alloc_stats);
1396 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1398 struct lprocfs_stats *stats = *statsh;
1399 unsigned int num_entry;
1400 unsigned int percpusize;
1403 if (!stats || stats->ls_num == 0)
1407 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1410 num_entry = num_possible_cpus();
1412 percpusize = lprocfs_stats_counter_size(stats);
1413 for (i = 0; i < num_entry; i++)
1414 if (stats->ls_percpu[i])
1415 LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1416 if (stats->ls_cnt_header)
1417 LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
1418 sizeof(struct lprocfs_counter_header));
1419 LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1421 EXPORT_SYMBOL(lprocfs_free_stats);
1423 u64 lprocfs_stats_collector(struct lprocfs_stats *stats, int idx,
1424 enum lprocfs_fields_flags field)
1426 unsigned long flags = 0;
1427 unsigned int num_cpu;
1433 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1434 for (i = 0; i < num_cpu; i++) {
1435 struct lprocfs_counter *cntr;
1437 if (!stats->ls_percpu[i])
1440 cntr = lprocfs_stats_counter_get(stats, i, idx);
1441 ret += lprocfs_read_helper(cntr, &stats->ls_cnt_header[idx],
1442 stats->ls_flags, field);
1444 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1447 EXPORT_SYMBOL(lprocfs_stats_collector);
1449 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1451 struct lprocfs_counter *percpu_cntr;
1454 unsigned int num_entry;
1455 unsigned long flags = 0;
1457 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1459 for (i = 0; i < num_entry; i++) {
1460 if (!stats->ls_percpu[i])
1462 for (j = 0; j < stats->ls_num; j++) {
1463 percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1464 percpu_cntr->lc_count = 0;
1465 percpu_cntr->lc_min = LC_MIN_INIT;
1466 percpu_cntr->lc_max = 0;
1467 percpu_cntr->lc_sumsquare = 0;
1468 percpu_cntr->lc_sum = 0;
1469 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1470 percpu_cntr->lc_sum_irq = 0;
1474 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1476 EXPORT_SYMBOL(lprocfs_clear_stats);
1478 static ssize_t lprocfs_stats_seq_write(struct file *file,
1479 const char __user *buf,
1480 size_t len, loff_t *off)
1482 struct seq_file *seq = file->private_data;
1483 struct lprocfs_stats *stats = seq->private;
1485 lprocfs_clear_stats(stats);
1490 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1492 struct lprocfs_stats *stats = p->private;
1494 return (*pos < stats->ls_num) ? pos : NULL;
1497 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1501 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1505 return lprocfs_stats_seq_start(p, pos);
1508 /* seq file export of one lprocfs counter */
1509 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1511 struct lprocfs_stats *stats = p->private;
1512 struct lprocfs_counter_header *hdr;
1513 struct lprocfs_counter ctr;
1514 int idx = *(loff_t *)v;
1517 struct timespec64 now;
1519 ktime_get_real_ts64(&now);
1520 seq_printf(p, "%-25s %llu.%09lu secs.nsecs\n",
1521 "snapshot_time", (s64)now.tv_sec, now.tv_nsec);
1524 hdr = &stats->ls_cnt_header[idx];
1525 lprocfs_stats_collect(stats, idx, &ctr);
1527 if (ctr.lc_count == 0)
1530 seq_printf(p, "%-25s %lld samples [%s]", hdr->lc_name,
1531 ctr.lc_count, hdr->lc_units);
1533 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1534 seq_printf(p, " %lld %lld %lld",
1535 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1536 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1537 seq_printf(p, " %llu", ctr.lc_sumsquare);
1543 static const struct seq_operations lprocfs_stats_seq_sops = {
1544 .start = lprocfs_stats_seq_start,
1545 .stop = lprocfs_stats_seq_stop,
1546 .next = lprocfs_stats_seq_next,
1547 .show = lprocfs_stats_seq_show,
1550 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1552 struct seq_file *seq;
1555 rc = LPROCFS_ENTRY_CHECK(inode);
1559 rc = seq_open(file, &lprocfs_stats_seq_sops);
1562 seq = file->private_data;
1563 seq->private = inode->i_private ? inode->i_private : PDE_DATA(inode);
1567 static const struct file_operations lprocfs_stats_seq_fops = {
1568 .owner = THIS_MODULE,
1569 .open = lprocfs_stats_seq_open,
1571 .write = lprocfs_stats_seq_write,
1572 .llseek = seq_lseek,
1573 .release = lprocfs_seq_release,
1576 int ldebugfs_register_stats(struct dentry *parent, const char *name,
1577 struct lprocfs_stats *stats)
1579 struct dentry *entry;
1581 LASSERT(!IS_ERR_OR_NULL(parent));
1583 entry = debugfs_create_file(name, 0644, parent, stats,
1584 &lprocfs_stats_seq_fops);
1585 if (IS_ERR_OR_NULL(entry))
1586 return entry ? PTR_ERR(entry) : -ENOMEM;
1590 EXPORT_SYMBOL_GPL(ldebugfs_register_stats);
1592 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1593 struct lprocfs_stats *stats)
1595 struct proc_dir_entry *entry;
1596 LASSERT(root != NULL);
1598 entry = proc_create_data(name, 0644, root,
1599 &lprocfs_stats_seq_fops, stats);
1604 EXPORT_SYMBOL(lprocfs_register_stats);
1606 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1607 unsigned conf, const char *name, const char *units)
1609 struct lprocfs_counter_header *header;
1610 struct lprocfs_counter *percpu_cntr;
1611 unsigned long flags = 0;
1613 unsigned int num_cpu;
1615 LASSERT(stats != NULL);
1617 header = &stats->ls_cnt_header[index];
1618 LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1619 index, name, units);
1621 header->lc_config = conf;
1622 header->lc_name = name;
1623 header->lc_units = units;
1625 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1626 for (i = 0; i < num_cpu; ++i) {
1627 if (!stats->ls_percpu[i])
1629 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1630 percpu_cntr->lc_count = 0;
1631 percpu_cntr->lc_min = LC_MIN_INIT;
1632 percpu_cntr->lc_max = 0;
1633 percpu_cntr->lc_sumsquare = 0;
1634 percpu_cntr->lc_sum = 0;
1635 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1636 percpu_cntr->lc_sum_irq = 0;
1638 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1640 EXPORT_SYMBOL(lprocfs_counter_init);
1642 static const char * const mps_stats[] = {
1643 [LPROC_MD_CLOSE] = "close",
1644 [LPROC_MD_CREATE] = "create",
1645 [LPROC_MD_ENQUEUE] = "enqueue",
1646 [LPROC_MD_GETATTR] = "getattr",
1647 [LPROC_MD_INTENT_LOCK] = "intent_lock",
1648 [LPROC_MD_LINK] = "link",
1649 [LPROC_MD_RENAME] = "rename",
1650 [LPROC_MD_SETATTR] = "setattr",
1651 [LPROC_MD_FSYNC] = "fsync",
1652 [LPROC_MD_READ_PAGE] = "read_page",
1653 [LPROC_MD_UNLINK] = "unlink",
1654 [LPROC_MD_SETXATTR] = "setxattr",
1655 [LPROC_MD_GETXATTR] = "getxattr",
1656 [LPROC_MD_INTENT_GETATTR_ASYNC] = "intent_getattr_async",
1657 [LPROC_MD_REVALIDATE_LOCK] = "revalidate_lock",
1660 int lprocfs_alloc_md_stats(struct obd_device *obd,
1661 unsigned int num_private_stats)
1663 struct lprocfs_stats *stats;
1664 unsigned int num_stats;
1668 * TODO Ensure that this function is only used where
1669 * appropriate by adding an assertion to the effect that
1670 * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1671 * because mdt_procfs_init() uses this function to allocate
1672 * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1673 * mdt layer does not use the md_ops interface. This is
1674 * confusing and a waste of memory. See LU-2484.
1676 LASSERT(obd->obd_proc_entry != NULL);
1677 LASSERT(obd->obd_md_stats == NULL);
1679 num_stats = ARRAY_SIZE(mps_stats) + num_private_stats;
1680 stats = lprocfs_alloc_stats(num_stats, 0);
1684 for (i = 0; i < ARRAY_SIZE(mps_stats); i++) {
1685 lprocfs_counter_init(stats, i, 0, mps_stats[i], "reqs");
1686 if (!stats->ls_cnt_header[i].lc_name) {
1687 CERROR("Missing md_stat initializer md_op operation at offset %d. Aborting.\n",
1693 rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1695 lprocfs_free_stats(&stats);
1697 obd->obd_md_stats = stats;
1702 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1704 void lprocfs_free_md_stats(struct obd_device *obd)
1706 struct lprocfs_stats *stats = obd->obd_md_stats;
1709 obd->obd_md_stats = NULL;
1710 lprocfs_free_stats(&stats);
1713 EXPORT_SYMBOL(lprocfs_free_md_stats);
1715 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1717 lprocfs_counter_init(ldlm_stats,
1718 LDLM_ENQUEUE - LDLM_FIRST_OPC,
1719 0, "ldlm_enqueue", "reqs");
1720 lprocfs_counter_init(ldlm_stats,
1721 LDLM_CONVERT - LDLM_FIRST_OPC,
1722 0, "ldlm_convert", "reqs");
1723 lprocfs_counter_init(ldlm_stats,
1724 LDLM_CANCEL - LDLM_FIRST_OPC,
1725 0, "ldlm_cancel", "reqs");
1726 lprocfs_counter_init(ldlm_stats,
1727 LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1728 0, "ldlm_bl_callback", "reqs");
1729 lprocfs_counter_init(ldlm_stats,
1730 LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1731 0, "ldlm_cp_callback", "reqs");
1732 lprocfs_counter_init(ldlm_stats,
1733 LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1734 0, "ldlm_gl_callback", "reqs");
1736 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1738 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1739 struct lprocfs_counter_header *header,
1740 enum lprocfs_stats_flags flags,
1741 enum lprocfs_fields_flags field)
1749 case LPROCFS_FIELDS_FLAGS_CONFIG:
1750 ret = header->lc_config;
1752 case LPROCFS_FIELDS_FLAGS_SUM:
1754 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1755 ret += lc->lc_sum_irq;
1757 case LPROCFS_FIELDS_FLAGS_MIN:
1760 case LPROCFS_FIELDS_FLAGS_MAX:
1763 case LPROCFS_FIELDS_FLAGS_AVG:
1764 ret = (lc->lc_max - lc->lc_min) / 2;
1766 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1767 ret = lc->lc_sumsquare;
1769 case LPROCFS_FIELDS_FLAGS_COUNT:
1777 EXPORT_SYMBOL(lprocfs_read_helper);
1779 /* Obtains the conversion factor for the unit specified */
1780 static int get_mult(char unit, __u64 *mult)
1785 /* peta, tera, giga, mega, and kilo */
1802 /* some tests expect % to be accepted */
1816 * Ensures the numeric string is valid. The function provides the final
1817 * multiplier in the case a unit exists at the end of the string. It also
1818 * locates the start of the whole and fractional parts (if any). This
1819 * function modifies the string so kstrtoull can be used to parse both
1820 * the whole and fraction portions. This function also figures out
1821 * the base of the number.
1823 static int preprocess_numeric_str(char *buffer, __u64 *mult, __u64 def_mult,
1824 bool allow_units, char **whole, char **frac,
1827 bool hit_decimal = false;
1828 bool hit_unit = false;
1836 /* a hex string if it starts with "0x" */
1837 if (buffer[0] == '0' && tolower(buffer[1]) == 'x') {
1845 /* allow for a single new line before the null terminator */
1846 if (*buffer == '\n') {
1856 /* any chars after our unit indicates a malformed string */
1860 /* ensure we only hit one decimal */
1861 if (*buffer == '.') {
1865 /* if past start, there's a whole part */
1866 if (start != buffer)
1872 } else if (!isdigit(*buffer) &&
1873 !(*base == 16 && isxdigit(*buffer))) {
1875 /* if we allow units, attempt to get mult */
1877 rc = get_mult(*buffer, mult);
1881 /* string stops here, but keep processing */
1893 /* hit a decimal, make sure there's a fractional part */
1899 /* didn't hit a decimal, but may have a whole part */
1900 if (start != buffer && *start)
1904 /* malformed string if we didn't get anything */
1905 if (!*frac && !*whole)
1912 * Parses a numeric string which can contain a whole and fraction portion
1913 * into a __u64. Accepts a multiplier to apply to the value parsed. Also
1914 * allows the string to have a unit at the end. The function handles
1915 * wrapping of the final unsigned value.
1917 static int str_to_u64_parse(char *buffer, unsigned long count,
1918 __u64 *val, __u64 def_mult, bool allow_units)
1922 unsigned int frac_d = 1;
1923 __u64 wrap_indicator = ULLONG_MAX;
1928 unsigned int base = 10;
1930 rc = preprocess_numeric_str(buffer, &mult, def_mult, allow_units,
1931 &strwhole, &strfrac, &base);
1941 /* the multiplier limits how large the value can be */
1942 wrap_indicator = div64_u64(wrap_indicator, mult);
1945 rc = kstrtoull(strwhole, base, &whole);
1949 if (whole > wrap_indicator)
1956 if (strlen(strfrac) > 10)
1959 rc = kstrtoull(strfrac, base, &frac);
1963 /* determine power of fractional portion */
1969 /* fractional portion is too large to perform calculation */
1970 if (frac > wrap_indicator)
1974 do_div(frac, frac_d);
1977 /* check that the sum of whole and fraction fits in u64 */
1978 if (whole > (ULLONG_MAX - frac))
1981 *val = whole + frac;
1987 * This function parses numeric/hex strings into __s64. It accepts a multiplier
1988 * which will apply to the value parsed. It also can allow the string to
1989 * have a unit as the last character. The function handles overflow/underflow
1990 * of the signed integer.
1992 static int str_to_s64_internal(const char __user *buffer, unsigned long count,
1993 __s64 *val, __u64 def_mult, bool allow_units)
1997 unsigned int offset = 0;
1998 int signed sign = 1;
1999 __u64 max = LLONG_MAX;
2002 if (count > (sizeof(kernbuf) - 1))
2005 if (copy_from_user(kernbuf, buffer, count))
2008 kernbuf[count] = '\0';
2010 /* keep track of our sign */
2011 if (*kernbuf == '-') {
2014 /* equivalent to max = -LLONG_MIN, avoids overflow */
2018 rc = str_to_u64_parse(kernbuf + offset, count - offset,
2019 &tmp, def_mult, allow_units);
2023 /* check for overflow/underflow */
2027 *val = (__s64)tmp * sign;
2033 * Convert a user string into a signed 64 bit number. This function produces
2034 * an error when the value parsed from the string times multiplier underflows or
2035 * overflows. This function only accepts strings that contains digits, an
2036 * optional decimal, and a char representing a unit at the end. If a unit is
2037 * specified in the string, the multiplier provided by the caller is ignored.
2038 * This function can also accept hexadecimal strings which are prefixed with
2041 * \param[in] buffer string consisting of numbers, a decimal, and a unit
2042 * \param[in] count buffer length
2043 * \param[in] val if successful, the value represented by the string
2044 * \param[in] defunit default unit if string doesn't contain one
2046 * \retval 0 on success
2047 * \retval negative number on error
2049 int lprocfs_str_with_units_to_s64(const char __user *buffer,
2050 unsigned long count, __s64 *val, char defunit)
2055 if (defunit != '1') {
2056 rc = get_mult(defunit, &mult);
2061 return str_to_s64_internal(buffer, count, val, mult, true);
2063 EXPORT_SYMBOL(lprocfs_str_with_units_to_s64);
2065 char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
2074 if (!memcmp(s1, s2, l2))
2080 EXPORT_SYMBOL(lprocfs_strnstr);
2083 * Find the string \a name in the input \a buffer, and return a pointer to the
2084 * value immediately following \a name, reducing \a count appropriately.
2085 * If \a name is not found the original \a buffer is returned.
2087 char *lprocfs_find_named_value(const char *buffer, const char *name,
2091 size_t buflen = *count;
2093 /* there is no strnstr() in rhel5 and ubuntu kernels */
2094 val = lprocfs_strnstr(buffer, name, buflen);
2096 return (char *)buffer;
2098 val += strlen(name); /* skip prefix */
2099 while (val < buffer + buflen && isspace(*val)) /* skip separator */
2103 while (val < buffer + buflen && isalnum(*val)) {
2108 return val - *count;
2110 EXPORT_SYMBOL(lprocfs_find_named_value);
2112 int ldebugfs_seq_create(struct dentry *parent, const char *name, umode_t mode,
2113 const struct file_operations *seq_fops, void *data)
2115 struct dentry *entry;
2117 /* Disallow secretly (un)writable entries. */
2118 LASSERT((!seq_fops->write) == (!(mode & 0222)));
2120 entry = debugfs_create_file(name, mode, parent, data, seq_fops);
2121 if (IS_ERR_OR_NULL(entry))
2122 return entry ? PTR_ERR(entry) : -ENOMEM;
2126 EXPORT_SYMBOL_GPL(ldebugfs_seq_create);
2128 int lprocfs_seq_create(struct proc_dir_entry *parent,
2131 const struct file_operations *seq_fops,
2134 struct proc_dir_entry *entry;
2137 /* Disallow secretly (un)writable entries. */
2138 LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
2140 entry = proc_create_data(name, mode, parent, seq_fops, data);
2147 EXPORT_SYMBOL(lprocfs_seq_create);
2149 int lprocfs_obd_seq_create(struct obd_device *dev,
2152 const struct file_operations *seq_fops,
2155 return (lprocfs_seq_create(dev->obd_proc_entry, name,
2156 mode, seq_fops, data));
2158 EXPORT_SYMBOL(lprocfs_obd_seq_create);
2160 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
2162 if (value >= OBD_HIST_MAX)
2163 value = OBD_HIST_MAX - 1;
2165 spin_lock(&oh->oh_lock);
2166 oh->oh_buckets[value]++;
2167 spin_unlock(&oh->oh_lock);
2169 EXPORT_SYMBOL(lprocfs_oh_tally);
2171 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
2173 unsigned int val = 0;
2175 if (likely(value != 0))
2176 val = min(fls(value - 1), OBD_HIST_MAX);
2178 lprocfs_oh_tally(oh, val);
2180 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
2182 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
2184 unsigned long ret = 0;
2187 for (i = 0; i < OBD_HIST_MAX; i++)
2188 ret += oh->oh_buckets[i];
2191 EXPORT_SYMBOL(lprocfs_oh_sum);
2193 void lprocfs_oh_clear(struct obd_histogram *oh)
2195 spin_lock(&oh->oh_lock);
2196 memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
2197 spin_unlock(&oh->oh_lock);
2199 EXPORT_SYMBOL(lprocfs_oh_clear);
2201 ssize_t lustre_attr_show(struct kobject *kobj,
2202 struct attribute *attr, char *buf)
2204 struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
2206 return a->show ? a->show(kobj, attr, buf) : 0;
2208 EXPORT_SYMBOL_GPL(lustre_attr_show);
2210 ssize_t lustre_attr_store(struct kobject *kobj, struct attribute *attr,
2211 const char *buf, size_t len)
2213 struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
2215 return a->store ? a->store(kobj, attr, buf, len) : len;
2217 EXPORT_SYMBOL_GPL(lustre_attr_store);
2219 const struct sysfs_ops lustre_sysfs_ops = {
2220 .show = lustre_attr_show,
2221 .store = lustre_attr_store,
2223 EXPORT_SYMBOL_GPL(lustre_sysfs_ops);
2225 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
2227 struct obd_device *dev = data;
2228 struct client_obd *cli = &dev->u.cli;
2230 spin_lock(&cli->cl_loi_list_lock);
2231 seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
2232 spin_unlock(&cli->cl_loi_list_lock);
2235 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
2237 ssize_t lprocfs_obd_max_pages_per_rpc_seq_write(struct file *file,
2238 const char __user *buffer,
2239 size_t count, loff_t *off)
2241 struct obd_device *dev =
2242 ((struct seq_file *)file->private_data)->private;
2243 struct client_obd *cli = &dev->u.cli;
2244 struct obd_connect_data *ocd = &cli->cl_import->imp_connect_data;
2248 rc = lprocfs_str_with_units_to_s64(buffer, count, &val, '1');
2254 /* if the max_pages is specified in bytes, convert to pages */
2255 if (val >= ONE_MB_BRW_SIZE)
2258 LPROCFS_CLIMP_CHECK(dev);
2260 chunk_mask = ~((1 << (cli->cl_chunkbits - PAGE_SHIFT)) - 1);
2261 /* max_pages_per_rpc must be chunk aligned */
2262 val = (val + ~chunk_mask) & chunk_mask;
2263 if (val == 0 || (ocd->ocd_brw_size != 0 &&
2264 val > ocd->ocd_brw_size >> PAGE_SHIFT)) {
2265 LPROCFS_CLIMP_EXIT(dev);
2268 spin_lock(&cli->cl_loi_list_lock);
2269 cli->cl_max_pages_per_rpc = val;
2270 client_adjust_max_dirty(cli);
2271 spin_unlock(&cli->cl_loi_list_lock);
2273 LPROCFS_CLIMP_EXIT(dev);
2276 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_write);
2278 ssize_t short_io_bytes_show(struct kobject *kobj, struct attribute *attr,
2281 struct obd_device *dev = container_of(kobj, struct obd_device,
2283 struct client_obd *cli = &dev->u.cli;
2286 spin_lock(&cli->cl_loi_list_lock);
2287 rc = sprintf(buf, "%d\n", cli->cl_max_short_io_bytes);
2288 spin_unlock(&cli->cl_loi_list_lock);
2291 EXPORT_SYMBOL(short_io_bytes_show);
2293 /* Used to catch people who think they're specifying pages. */
2294 #define MIN_SHORT_IO_BYTES 64U
2296 ssize_t short_io_bytes_store(struct kobject *kobj, struct attribute *attr,
2297 const char *buffer, size_t count)
2299 struct obd_device *dev = container_of(kobj, struct obd_device,
2301 struct client_obd *cli = &dev->u.cli;
2305 LPROCFS_CLIMP_CHECK(dev);
2307 rc = kstrtouint(buffer, 0, &val);
2311 if (val && (val < MIN_SHORT_IO_BYTES || val > OBD_MAX_SHORT_IO_BYTES))
2312 GOTO(out, rc = -ERANGE);
2316 spin_lock(&cli->cl_loi_list_lock);
2317 if (val > (cli->cl_max_pages_per_rpc << PAGE_SHIFT))
2320 cli->cl_max_short_io_bytes = val;
2321 spin_unlock(&cli->cl_loi_list_lock);
2324 LPROCFS_CLIMP_EXIT(dev);
2327 EXPORT_SYMBOL(short_io_bytes_store);
2329 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2330 struct root_squash_info *squash, char *name)
2333 char kernbuf[64], *tmp, *errmsg;
2334 unsigned long uid, gid;
2337 if (count >= sizeof(kernbuf)) {
2338 errmsg = "string too long";
2339 GOTO(failed_noprint, rc = -EINVAL);
2341 if (copy_from_user(kernbuf, buffer, count)) {
2342 errmsg = "bad address";
2343 GOTO(failed_noprint, rc = -EFAULT);
2345 kernbuf[count] = '\0';
2347 /* look for uid gid separator */
2348 tmp = strchr(kernbuf, ':');
2350 errmsg = "needs uid:gid format";
2351 GOTO(failed, rc = -EINVAL);
2357 if (kstrtoul(kernbuf, 0, &uid) != 0) {
2359 GOTO(failed, rc = -EINVAL);
2363 if (kstrtoul(tmp, 0, &gid) != 0) {
2365 GOTO(failed, rc = -EINVAL);
2368 squash->rsi_uid = uid;
2369 squash->rsi_gid = gid;
2371 LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2372 name, squash->rsi_uid, squash->rsi_gid);
2380 CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2381 name, kernbuf, errmsg, rc);
2384 CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2388 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2391 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2392 struct root_squash_info *squash, char *name)
2395 char *kernbuf = NULL;
2397 struct list_head tmp;
2402 errmsg = "string too long";
2403 GOTO(failed, rc = -EINVAL);
2406 OBD_ALLOC(kernbuf, count + 1);
2408 errmsg = "no memory";
2409 GOTO(failed, rc = -ENOMEM);
2411 if (copy_from_user(kernbuf, buffer, count)) {
2412 errmsg = "bad address";
2413 GOTO(failed, rc = -EFAULT);
2415 kernbuf[count] = '\0';
2417 if (count > 0 && kernbuf[count - 1] == '\n')
2420 if ((len == 4 && strncmp(kernbuf, "NONE", len) == 0) ||
2421 (len == 5 && strncmp(kernbuf, "clear", len) == 0)) {
2422 /* empty string is special case */
2423 down_write(&squash->rsi_sem);
2424 if (!list_empty(&squash->rsi_nosquash_nids))
2425 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2426 up_write(&squash->rsi_sem);
2427 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2428 OBD_FREE(kernbuf, count + 1);
2432 INIT_LIST_HEAD(&tmp);
2433 if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2434 errmsg = "can't parse";
2435 GOTO(failed, rc = -EINVAL);
2437 LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2439 OBD_FREE(kernbuf, count + 1);
2442 down_write(&squash->rsi_sem);
2443 if (!list_empty(&squash->rsi_nosquash_nids))
2444 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2445 list_splice(&tmp, &squash->rsi_nosquash_nids);
2446 up_write(&squash->rsi_sem);
2452 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2453 name, kernbuf, errmsg, rc);
2454 OBD_FREE(kernbuf, count + 1);
2456 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2461 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2463 #endif /* CONFIG_PROC_FS*/