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 result *= rounddown_pow_of_two(blk_size ?: 1);
429 return sprintf(buf, "%llu\n", result);
434 LUSTRE_RO_ATTR(kbytestotal);
436 static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
439 struct obd_device *obd = container_of(kobj, struct obd_device,
441 struct obd_statfs osfs;
444 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
445 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
448 u32 blk_size = osfs.os_bsize >> 10;
449 u64 result = osfs.os_bfree;
451 while (blk_size >>= 1)
454 return sprintf(buf, "%llu\n", result);
459 LUSTRE_RO_ATTR(kbytesfree);
461 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
464 struct obd_device *obd = container_of(kobj, struct obd_device,
466 struct obd_statfs osfs;
469 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
470 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
473 u32 blk_size = osfs.os_bsize >> 10;
474 u64 result = osfs.os_bavail;
476 while (blk_size >>= 1)
479 return sprintf(buf, "%llu\n", result);
484 LUSTRE_RO_ATTR(kbytesavail);
486 static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
489 struct obd_device *obd = container_of(kobj, struct obd_device,
491 struct obd_statfs osfs;
494 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
495 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
498 return sprintf(buf, "%llu\n", osfs.os_files);
502 LUSTRE_RO_ATTR(filestotal);
504 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
507 struct obd_device *obd = container_of(kobj, struct obd_device,
509 struct obd_statfs osfs;
512 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
513 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
516 return sprintf(buf, "%llu\n", osfs.os_ffree);
520 LUSTRE_RO_ATTR(filesfree);
522 ssize_t conn_uuid_show(struct kobject *kobj, struct attribute *attr, char *buf)
524 struct obd_device *obd = container_of(kobj, struct obd_device,
526 struct ptlrpc_connection *conn;
529 LPROCFS_CLIMP_CHECK(obd);
530 conn = obd->u.cli.cl_import->imp_connection;
531 if (conn && obd->u.cli.cl_import)
532 count = sprintf(buf, "%s\n", conn->c_remote_uuid.uuid);
534 count = sprintf(buf, "%s\n", "<none>");
536 LPROCFS_CLIMP_EXIT(obd);
539 EXPORT_SYMBOL(conn_uuid_show);
541 int lprocfs_server_uuid_seq_show(struct seq_file *m, void *data)
543 struct obd_device *obd = data;
544 struct obd_import *imp;
545 char *imp_state_name = NULL;
548 LASSERT(obd != NULL);
549 LPROCFS_CLIMP_CHECK(obd);
550 imp = obd->u.cli.cl_import;
551 imp_state_name = ptlrpc_import_state_name(imp->imp_state);
552 seq_printf(m, "%s\t%s%s\n", obd2cli_tgt(obd), imp_state_name,
553 imp->imp_deactive ? "\tDEACTIVATED" : "");
555 LPROCFS_CLIMP_EXIT(obd);
558 EXPORT_SYMBOL(lprocfs_server_uuid_seq_show);
560 /** add up per-cpu counters */
563 * Lock statistics structure for access, possibly only on this CPU.
565 * The statistics struct may be allocated with per-CPU structures for
566 * efficient concurrent update (usually only on server-wide stats), or
567 * as a single global struct (e.g. for per-client or per-job statistics),
568 * so the required locking depends on the type of structure allocated.
570 * For per-CPU statistics, pin the thread to the current cpuid so that
571 * will only access the statistics for that CPU. If the stats structure
572 * for the current CPU has not been allocated (or previously freed),
573 * allocate it now. The per-CPU statistics do not need locking since
574 * the thread is pinned to the CPU during update.
576 * For global statistics, lock the stats structure to prevent concurrent update.
578 * \param[in] stats statistics structure to lock
579 * \param[in] opc type of operation:
580 * LPROCFS_GET_SMP_ID: "lock" and return current CPU index
581 * for incrementing statistics for that CPU
582 * LPROCFS_GET_NUM_CPU: "lock" and return number of used
583 * CPU indices to iterate over all indices
584 * \param[out] flags CPU interrupt saved state for IRQ-safe locking
586 * \retval cpuid of current thread or number of allocated structs
587 * \retval negative on error (only for opc LPROCFS_GET_SMP_ID + per-CPU stats)
589 int lprocfs_stats_lock(struct lprocfs_stats *stats,
590 enum lprocfs_stats_lock_ops opc,
591 unsigned long *flags)
593 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
594 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
595 spin_lock_irqsave(&stats->ls_lock, *flags);
597 spin_lock(&stats->ls_lock);
598 return opc == LPROCFS_GET_NUM_CPU ? 1 : 0;
602 case LPROCFS_GET_SMP_ID: {
603 unsigned int cpuid = get_cpu();
605 if (unlikely(!stats->ls_percpu[cpuid])) {
606 int rc = lprocfs_stats_alloc_one(stats, cpuid);
615 case LPROCFS_GET_NUM_CPU:
616 return stats->ls_biggest_alloc_num;
623 * Unlock statistics structure after access.
625 * Unlock the lock acquired via lprocfs_stats_lock() for global statistics,
626 * or unpin this thread from the current cpuid for per-CPU statistics.
628 * This function must be called using the same arguments as used when calling
629 * lprocfs_stats_lock() so that the correct operation can be performed.
631 * \param[in] stats statistics structure to unlock
632 * \param[in] opc type of operation (current cpuid or number of structs)
633 * \param[in] flags CPU interrupt saved state for IRQ-safe locking
635 void lprocfs_stats_unlock(struct lprocfs_stats *stats,
636 enum lprocfs_stats_lock_ops opc,
637 unsigned long *flags)
639 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
640 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
641 spin_unlock_irqrestore(&stats->ls_lock, *flags);
643 spin_unlock(&stats->ls_lock);
644 } else if (opc == LPROCFS_GET_SMP_ID) {
649 /** add up per-cpu counters */
650 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
651 struct lprocfs_counter *cnt)
653 unsigned int num_entry;
654 struct lprocfs_counter *percpu_cntr;
656 unsigned long flags = 0;
658 memset(cnt, 0, sizeof(*cnt));
661 /* set count to 1 to avoid divide-by-zero errs in callers */
666 cnt->lc_min = LC_MIN_INIT;
668 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
670 for (i = 0; i < num_entry; i++) {
671 if (!stats->ls_percpu[i])
673 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
675 cnt->lc_count += percpu_cntr->lc_count;
676 cnt->lc_sum += percpu_cntr->lc_sum;
677 if (percpu_cntr->lc_min < cnt->lc_min)
678 cnt->lc_min = percpu_cntr->lc_min;
679 if (percpu_cntr->lc_max > cnt->lc_max)
680 cnt->lc_max = percpu_cntr->lc_max;
681 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
684 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
687 static void obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
691 if (imp->imp_obd->obd_no_recov) {
692 seq_printf(m, "no_recov");
696 flag2str(imp, invalid);
697 flag2str(imp, deactive);
698 flag2str(imp, replayable);
699 flag2str(imp, delayed_recovery);
700 flag2str(imp, vbr_failed);
701 flag2str(imp, pingable);
702 flag2str(imp, resend_replay);
703 flag2str(imp, no_pinger_recover);
704 flag2str(imp, connect_tried);
707 static const char *obd_connect_names[] = {
726 "remote_client_by_force",
735 "mds_mds_connection",
738 "alt_checksum_algorithm",
774 "file_secctx", /* 0x01 */
775 "lockaheadv2", /* 0x02 */
776 "dir_migrate", /* 0x04 */
777 "sum_statfs", /* 0x08 */
778 "overstriping", /* 0x10 */
781 "lock_convert", /* 0x80 */
782 "archive_id_array", /* 0x100 */
783 "increasing_xid", /* 0x200 */
784 "selinux_policy", /* 0x400 */
787 "plain_layout", /* 0x2000 */
788 "async_discard", /* 0x4000 */
789 "client_encryption", /* 0x8000 */
793 void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, __u64 flags2,
800 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
802 seq_printf(m, "%s%s",
803 first ? "" : sep, obd_connect_names[i]);
808 if (flags & ~(mask - 1)) {
809 seq_printf(m, "%sunknown_%#llx",
810 first ? "" : sep, flags & ~(mask - 1));
814 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
817 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
819 seq_printf(m, "%s%s",
820 first ? "" : sep, obd_connect_names[i]);
825 if (flags2 & ~(mask - 1)) {
826 seq_printf(m, "%sunknown2_%#llx",
827 first ? "" : sep, flags2 & ~(mask - 1));
831 EXPORT_SYMBOL(obd_connect_seq_flags2str);
833 int obd_connect_flags2str(char *page, int count, __u64 flags, __u64 flags2,
839 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
841 ret += snprintf(page + ret, count - ret, "%s%s",
842 ret ? sep : "", obd_connect_names[i]);
845 if (flags & ~(mask - 1))
846 ret += snprintf(page + ret, count - ret,
848 ret ? sep : "", flags & ~(mask - 1));
850 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
853 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
855 ret += snprintf(page + ret, count - ret, "%s%s",
856 ret ? sep : "", obd_connect_names[i]);
859 if (flags2 & ~(mask - 1))
860 ret += snprintf(page + ret, count - ret,
862 ret ? sep : "", flags2 & ~(mask - 1));
866 EXPORT_SYMBOL(obd_connect_flags2str);
869 obd_connect_data_seqprint(struct seq_file *m, struct obd_connect_data *ocd)
873 LASSERT(ocd != NULL);
874 flags = ocd->ocd_connect_flags;
876 seq_printf(m, " connect_data:\n"
879 ocd->ocd_connect_flags,
881 if (flags & OBD_CONNECT_VERSION)
882 seq_printf(m, " target_version: %u.%u.%u.%u\n",
883 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
884 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
885 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
886 OBD_OCD_VERSION_FIX(ocd->ocd_version));
887 if (flags & OBD_CONNECT_MDS)
888 seq_printf(m, " mdt_index: %d\n", ocd->ocd_group);
889 if (flags & OBD_CONNECT_GRANT)
890 seq_printf(m, " initial_grant: %d\n", ocd->ocd_grant);
891 if (flags & OBD_CONNECT_INDEX)
892 seq_printf(m, " target_index: %u\n", ocd->ocd_index);
893 if (flags & OBD_CONNECT_BRW_SIZE)
894 seq_printf(m, " max_brw_size: %d\n", ocd->ocd_brw_size);
895 if (flags & OBD_CONNECT_IBITS)
896 seq_printf(m, " ibits_known: %#llx\n",
897 ocd->ocd_ibits_known);
898 if (flags & OBD_CONNECT_GRANT_PARAM)
899 seq_printf(m, " grant_block_size: %d\n"
900 " grant_inode_size: %d\n"
901 " grant_max_extent_size: %d\n"
902 " grant_extent_tax: %d\n",
903 1 << ocd->ocd_grant_blkbits,
904 1 << ocd->ocd_grant_inobits,
905 ocd->ocd_grant_max_blks << ocd->ocd_grant_blkbits,
906 ocd->ocd_grant_tax_kb << 10);
907 if (flags & OBD_CONNECT_TRANSNO)
908 seq_printf(m, " first_transno: %#llx\n",
910 if (flags & OBD_CONNECT_CKSUM)
911 seq_printf(m, " cksum_types: %#x\n",
912 ocd->ocd_cksum_types);
913 if (flags & OBD_CONNECT_MAX_EASIZE)
914 seq_printf(m, " max_easize: %d\n", ocd->ocd_max_easize);
915 if (flags & OBD_CONNECT_MAXBYTES)
916 seq_printf(m, " max_object_bytes: %llu\n",
918 if (flags & OBD_CONNECT_MULTIMODRPCS)
919 seq_printf(m, " max_mod_rpcs: %hu\n",
920 ocd->ocd_maxmodrpcs);
923 int lprocfs_import_seq_show(struct seq_file *m, void *data)
925 char nidstr[LNET_NIDSTR_SIZE];
926 struct lprocfs_counter ret;
927 struct lprocfs_counter_header *header;
928 struct obd_device *obd = (struct obd_device *)data;
929 struct obd_import *imp;
930 struct obd_import_conn *conn;
931 struct obd_connect_data *ocd;
936 LASSERT(obd != NULL);
937 LPROCFS_CLIMP_CHECK(obd);
938 imp = obd->u.cli.cl_import;
939 ocd = &imp->imp_connect_data;
941 seq_printf(m, "import:\n"
945 " connect_flags: [ ",
948 ptlrpc_import_state_name(imp->imp_state));
949 obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
950 imp->imp_connect_data.ocd_connect_flags2,
952 seq_printf(m, " ]\n");
953 obd_connect_data_seqprint(m, ocd);
954 seq_printf(m, " import_flags: [ ");
955 obd_import_flags2str(imp, m);
959 " failover_nids: [ ");
960 spin_lock(&imp->imp_lock);
962 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
963 libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
964 nidstr, sizeof(nidstr));
965 seq_printf(m, "%s%s", j ? ", " : "", nidstr);
968 if (imp->imp_connection)
969 libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
970 nidstr, sizeof(nidstr));
972 strncpy(nidstr, "<none>", sizeof(nidstr));
974 " current_connection: %s\n"
975 " connection_attempts: %u\n"
977 " in-progress_invalidations: %u\n"
982 atomic_read(&imp->imp_inval_count),
983 ktime_get_real_seconds() - imp->imp_last_reply_time);
984 spin_unlock(&imp->imp_lock);
986 if (!obd->obd_svc_stats)
989 header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
990 lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
991 if (ret.lc_count != 0) {
992 /* first argument to do_div MUST be __u64 */
993 __u64 sum = ret.lc_sum;
994 do_div(sum, ret.lc_count);
998 seq_printf(m, " rpcs:\n"
1000 " unregistering: %u\n"
1002 " avg_waittime: %llu %s\n",
1003 atomic_read(&imp->imp_inflight),
1004 atomic_read(&imp->imp_unregistering),
1005 atomic_read(&imp->imp_timeouts),
1006 ret.lc_sum, header->lc_units);
1009 for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
1010 if (imp->imp_at.iat_portal[j] == 0)
1012 k = max_t(unsigned int, k,
1013 at_get(&imp->imp_at.iat_service_estimate[j]));
1015 seq_printf(m, " service_estimates:\n"
1016 " services: %u sec\n"
1017 " network: %u sec\n",
1019 at_get(&imp->imp_at.iat_net_latency));
1021 seq_printf(m, " transactions:\n"
1022 " last_replay: %llu\n"
1023 " peer_committed: %llu\n"
1024 " last_checked: %llu\n",
1025 imp->imp_last_replay_transno,
1026 imp->imp_peer_committed_transno,
1027 imp->imp_last_transno_checked);
1029 /* avg data rates */
1030 for (rw = 0; rw <= 1; rw++) {
1031 lprocfs_stats_collect(obd->obd_svc_stats,
1032 PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
1034 if (ret.lc_sum > 0 && ret.lc_count > 0) {
1035 /* first argument to do_div MUST be __u64 */
1036 __u64 sum = ret.lc_sum;
1037 do_div(sum, ret.lc_count);
1039 seq_printf(m, " %s_data_averages:\n"
1040 " bytes_per_rpc: %llu\n",
1041 rw ? "write" : "read",
1044 k = (int)ret.lc_sum;
1045 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
1046 header = &obd->obd_svc_stats->ls_cnt_header[j];
1047 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
1048 if (ret.lc_sum > 0 && ret.lc_count != 0) {
1049 /* first argument to do_div MUST be __u64 */
1050 __u64 sum = ret.lc_sum;
1051 do_div(sum, ret.lc_count);
1053 seq_printf(m, " %s_per_rpc: %llu\n",
1054 header->lc_units, ret.lc_sum);
1055 j = (int)ret.lc_sum;
1057 seq_printf(m, " MB_per_sec: %u.%.02u\n",
1058 k / j, (100 * k / j) % 100);
1063 LPROCFS_CLIMP_EXIT(obd);
1066 EXPORT_SYMBOL(lprocfs_import_seq_show);
1068 int lprocfs_state_seq_show(struct seq_file *m, void *data)
1070 struct obd_device *obd = (struct obd_device *)data;
1071 struct obd_import *imp;
1074 LASSERT(obd != NULL);
1075 LPROCFS_CLIMP_CHECK(obd);
1076 imp = obd->u.cli.cl_import;
1078 seq_printf(m, "current_state: %s\n",
1079 ptlrpc_import_state_name(imp->imp_state));
1080 seq_printf(m, "state_history:\n");
1081 k = imp->imp_state_hist_idx;
1082 for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
1083 struct import_state_hist *ish =
1084 &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
1085 if (ish->ish_state == 0)
1087 seq_printf(m, " - [ %lld, %s ]\n", (s64)ish->ish_time,
1088 ptlrpc_import_state_name(ish->ish_state));
1091 LPROCFS_CLIMP_EXIT(obd);
1094 EXPORT_SYMBOL(lprocfs_state_seq_show);
1096 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
1099 for (i = 0; i < AT_BINS; i++)
1100 seq_printf(m, "%3u ", at->at_hist[i]);
1101 seq_printf(m, "\n");
1104 EXPORT_SYMBOL(lprocfs_at_hist_helper);
1106 /* See also ptlrpc_lprocfs_timeouts_show_seq */
1107 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
1109 struct obd_device *obd = (struct obd_device *)data;
1110 struct obd_import *imp;
1111 unsigned int cur, worst;
1112 time64_t now, worstt;
1115 LASSERT(obd != NULL);
1116 LPROCFS_CLIMP_CHECK(obd);
1117 imp = obd->u.cli.cl_import;
1119 now = ktime_get_real_seconds();
1121 /* Some network health info for kicks */
1122 seq_printf(m, "%-10s : %lld, %llds ago\n",
1123 "last reply", (s64)imp->imp_last_reply_time,
1124 (s64)(now - imp->imp_last_reply_time));
1126 cur = at_get(&imp->imp_at.iat_net_latency);
1127 worst = imp->imp_at.iat_net_latency.at_worst_ever;
1128 worstt = imp->imp_at.iat_net_latency.at_worst_time;
1129 seq_printf(m, "%-10s : cur %3u worst %3u (at %lld, %llds ago) ",
1130 "network", cur, worst, (s64)worstt, (s64)(now - worstt));
1131 lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
1133 for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1134 if (imp->imp_at.iat_portal[i] == 0)
1136 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
1137 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
1138 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
1139 seq_printf(m, "portal %-2d : cur %3u worst %3u (at %lld, %llds ago) ",
1140 imp->imp_at.iat_portal[i], cur, worst, (s64)worstt,
1141 (s64)(now - worstt));
1142 lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
1145 LPROCFS_CLIMP_EXIT(obd);
1148 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
1150 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
1152 struct obd_device *obd = data;
1156 LPROCFS_CLIMP_CHECK(obd);
1157 flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
1158 flags2 = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags2;
1159 seq_printf(m, "flags=%#llx\n", flags);
1160 seq_printf(m, "flags2=%#llx\n", flags2);
1161 obd_connect_seq_flags2str(m, flags, flags2, "\n");
1162 seq_printf(m, "\n");
1163 LPROCFS_CLIMP_EXIT(obd);
1166 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
1168 static const struct attribute *obd_def_uuid_attrs[] = {
1169 &lustre_attr_uuid.attr,
1173 static const struct attribute *obd_def_attrs[] = {
1174 &lustre_attr_blocksize.attr,
1175 &lustre_attr_kbytestotal.attr,
1176 &lustre_attr_kbytesfree.attr,
1177 &lustre_attr_kbytesavail.attr,
1178 &lustre_attr_filestotal.attr,
1179 &lustre_attr_filesfree.attr,
1180 &lustre_attr_uuid.attr,
1184 static void obd_sysfs_release(struct kobject *kobj)
1186 struct obd_device *obd = container_of(kobj, struct obd_device,
1189 complete(&obd->obd_kobj_unregister);
1192 int lprocfs_obd_setup(struct obd_device *obd, bool uuid_only)
1194 struct lprocfs_vars *debugfs_vars = NULL;
1197 if (!obd || obd->obd_magic != OBD_DEVICE_MAGIC)
1200 rc = kobject_set_name(&obd->obd_kset.kobj, "%s", obd->obd_name);
1204 obd->obd_ktype.sysfs_ops = &lustre_sysfs_ops;
1205 obd->obd_ktype.release = obd_sysfs_release;
1207 obd->obd_kset.kobj.parent = &obd->obd_type->typ_kobj;
1208 obd->obd_kset.kobj.ktype = &obd->obd_ktype;
1209 init_completion(&obd->obd_kobj_unregister);
1210 rc = kset_register(&obd->obd_kset);
1215 obd->obd_attrs = obd_def_uuid_attrs;
1217 obd->obd_attrs = obd_def_attrs;
1219 rc = sysfs_create_files(&obd->obd_kset.kobj, obd->obd_attrs);
1221 kset_unregister(&obd->obd_kset);
1225 if (!obd->obd_type->typ_procroot)
1226 debugfs_vars = obd->obd_vars;
1227 obd->obd_debugfs_entry = ldebugfs_register(obd->obd_name,
1228 obd->obd_type->typ_debugfs_entry,
1230 if (IS_ERR_OR_NULL(obd->obd_debugfs_entry)) {
1231 rc = obd->obd_debugfs_entry ? PTR_ERR(obd->obd_debugfs_entry)
1233 CERROR("error %d setting up debugfs for %s\n",
1235 obd->obd_debugfs_entry = NULL;
1237 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1238 obd->obd_attrs = NULL;
1239 kset_unregister(&obd->obd_kset);
1243 if (obd->obd_proc_entry || !obd->obd_type->typ_procroot)
1244 GOTO(already_registered, rc);
1246 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1247 obd->obd_type->typ_procroot,
1248 obd->obd_vars, obd);
1249 if (IS_ERR(obd->obd_proc_entry)) {
1250 rc = PTR_ERR(obd->obd_proc_entry);
1251 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1252 obd->obd_proc_entry = NULL;
1254 ldebugfs_remove(&obd->obd_debugfs_entry);
1255 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1256 obd->obd_attrs = NULL;
1257 kset_unregister(&obd->obd_kset);
1263 EXPORT_SYMBOL(lprocfs_obd_setup);
1265 int lprocfs_obd_cleanup(struct obd_device *obd)
1270 if (obd->obd_proc_exports_entry) {
1271 /* Should be no exports left */
1272 lprocfs_remove(&obd->obd_proc_exports_entry);
1273 obd->obd_proc_exports_entry = NULL;
1276 if (obd->obd_proc_entry) {
1277 lprocfs_remove(&obd->obd_proc_entry);
1278 obd->obd_proc_entry = NULL;
1281 if (!IS_ERR_OR_NULL(obd->obd_debugfs_entry))
1282 ldebugfs_remove(&obd->obd_debugfs_entry);
1284 /* obd device never allocated a kset */
1285 if (!obd->obd_kset.kobj.state_initialized)
1288 if (obd->obd_attrs) {
1289 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1290 obd->obd_attrs = NULL;
1293 kset_unregister(&obd->obd_kset);
1294 wait_for_completion(&obd->obd_kobj_unregister);
1297 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1299 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1301 struct lprocfs_counter *cntr;
1302 unsigned int percpusize;
1304 unsigned long flags = 0;
1307 LASSERT(stats->ls_percpu[cpuid] == NULL);
1308 LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1310 percpusize = lprocfs_stats_counter_size(stats);
1311 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1312 if (stats->ls_percpu[cpuid]) {
1314 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1315 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1316 spin_lock_irqsave(&stats->ls_lock, flags);
1318 spin_lock(&stats->ls_lock);
1319 if (stats->ls_biggest_alloc_num <= cpuid)
1320 stats->ls_biggest_alloc_num = cpuid + 1;
1321 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1322 spin_unlock_irqrestore(&stats->ls_lock, flags);
1324 spin_unlock(&stats->ls_lock);
1327 /* initialize the ls_percpu[cpuid] non-zero counter */
1328 for (i = 0; i < stats->ls_num; ++i) {
1329 cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1330 cntr->lc_min = LC_MIN_INIT;
1336 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1337 enum lprocfs_stats_flags flags)
1339 struct lprocfs_stats *stats;
1340 unsigned int num_entry;
1341 unsigned int percpusize = 0;
1347 if (lprocfs_no_percpu_stats != 0)
1348 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1350 if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1353 num_entry = num_possible_cpus();
1355 /* alloc percpu pointers for all possible cpu slots */
1356 LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1360 stats->ls_num = num;
1361 stats->ls_flags = flags;
1362 spin_lock_init(&stats->ls_lock);
1364 /* alloc num of counter headers */
1365 LIBCFS_ALLOC(stats->ls_cnt_header,
1366 stats->ls_num * sizeof(struct lprocfs_counter_header));
1367 if (!stats->ls_cnt_header)
1370 if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1371 /* contains only one set counters */
1372 percpusize = lprocfs_stats_counter_size(stats);
1373 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1374 if (!stats->ls_percpu[0])
1376 stats->ls_biggest_alloc_num = 1;
1377 } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1378 /* alloc all percpu data, currently only obd_memory use this */
1379 for (i = 0; i < num_entry; ++i)
1380 if (lprocfs_stats_alloc_one(stats, i) < 0)
1387 lprocfs_free_stats(&stats);
1390 EXPORT_SYMBOL(lprocfs_alloc_stats);
1392 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1394 struct lprocfs_stats *stats = *statsh;
1395 unsigned int num_entry;
1396 unsigned int percpusize;
1399 if (!stats || stats->ls_num == 0)
1403 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1406 num_entry = num_possible_cpus();
1408 percpusize = lprocfs_stats_counter_size(stats);
1409 for (i = 0; i < num_entry; i++)
1410 if (stats->ls_percpu[i])
1411 LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1412 if (stats->ls_cnt_header)
1413 LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
1414 sizeof(struct lprocfs_counter_header));
1415 LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1417 EXPORT_SYMBOL(lprocfs_free_stats);
1419 u64 lprocfs_stats_collector(struct lprocfs_stats *stats, int idx,
1420 enum lprocfs_fields_flags field)
1422 unsigned long flags = 0;
1423 unsigned int num_cpu;
1429 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1430 for (i = 0; i < num_cpu; i++) {
1431 struct lprocfs_counter *cntr;
1433 if (!stats->ls_percpu[i])
1436 cntr = lprocfs_stats_counter_get(stats, i, idx);
1437 ret += lprocfs_read_helper(cntr, &stats->ls_cnt_header[idx],
1438 stats->ls_flags, field);
1440 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1443 EXPORT_SYMBOL(lprocfs_stats_collector);
1445 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1447 struct lprocfs_counter *percpu_cntr;
1450 unsigned int num_entry;
1451 unsigned long flags = 0;
1453 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1455 for (i = 0; i < num_entry; i++) {
1456 if (!stats->ls_percpu[i])
1458 for (j = 0; j < stats->ls_num; j++) {
1459 percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1460 percpu_cntr->lc_count = 0;
1461 percpu_cntr->lc_min = LC_MIN_INIT;
1462 percpu_cntr->lc_max = 0;
1463 percpu_cntr->lc_sumsquare = 0;
1464 percpu_cntr->lc_sum = 0;
1465 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1466 percpu_cntr->lc_sum_irq = 0;
1470 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1472 EXPORT_SYMBOL(lprocfs_clear_stats);
1474 static ssize_t lprocfs_stats_seq_write(struct file *file,
1475 const char __user *buf,
1476 size_t len, loff_t *off)
1478 struct seq_file *seq = file->private_data;
1479 struct lprocfs_stats *stats = seq->private;
1481 lprocfs_clear_stats(stats);
1486 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1488 struct lprocfs_stats *stats = p->private;
1490 return (*pos < stats->ls_num) ? pos : NULL;
1493 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1497 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1501 return lprocfs_stats_seq_start(p, pos);
1504 /* seq file export of one lprocfs counter */
1505 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1507 struct lprocfs_stats *stats = p->private;
1508 struct lprocfs_counter_header *hdr;
1509 struct lprocfs_counter ctr;
1510 int idx = *(loff_t *)v;
1513 struct timespec64 now;
1515 ktime_get_real_ts64(&now);
1516 seq_printf(p, "%-25s %llu.%09lu secs.nsecs\n",
1517 "snapshot_time", (s64)now.tv_sec, now.tv_nsec);
1520 hdr = &stats->ls_cnt_header[idx];
1521 lprocfs_stats_collect(stats, idx, &ctr);
1523 if (ctr.lc_count == 0)
1526 seq_printf(p, "%-25s %lld samples [%s]", hdr->lc_name,
1527 ctr.lc_count, hdr->lc_units);
1529 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1530 seq_printf(p, " %lld %lld %lld",
1531 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1532 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1533 seq_printf(p, " %llu", ctr.lc_sumsquare);
1539 static const struct seq_operations lprocfs_stats_seq_sops = {
1540 .start = lprocfs_stats_seq_start,
1541 .stop = lprocfs_stats_seq_stop,
1542 .next = lprocfs_stats_seq_next,
1543 .show = lprocfs_stats_seq_show,
1546 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1548 struct seq_file *seq;
1551 rc = LPROCFS_ENTRY_CHECK(inode);
1555 rc = seq_open(file, &lprocfs_stats_seq_sops);
1558 seq = file->private_data;
1559 seq->private = inode->i_private ? inode->i_private : PDE_DATA(inode);
1563 static const struct file_operations lprocfs_stats_seq_fops = {
1564 .owner = THIS_MODULE,
1565 .open = lprocfs_stats_seq_open,
1567 .write = lprocfs_stats_seq_write,
1568 .llseek = seq_lseek,
1569 .release = lprocfs_seq_release,
1572 int ldebugfs_register_stats(struct dentry *parent, const char *name,
1573 struct lprocfs_stats *stats)
1575 struct dentry *entry;
1577 LASSERT(!IS_ERR_OR_NULL(parent));
1579 entry = debugfs_create_file(name, 0644, parent, stats,
1580 &lprocfs_stats_seq_fops);
1581 if (IS_ERR_OR_NULL(entry))
1582 return entry ? PTR_ERR(entry) : -ENOMEM;
1586 EXPORT_SYMBOL_GPL(ldebugfs_register_stats);
1588 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1589 struct lprocfs_stats *stats)
1591 struct proc_dir_entry *entry;
1592 LASSERT(root != NULL);
1594 entry = proc_create_data(name, 0644, root,
1595 &lprocfs_stats_seq_fops, stats);
1600 EXPORT_SYMBOL(lprocfs_register_stats);
1602 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1603 unsigned conf, const char *name, const char *units)
1605 struct lprocfs_counter_header *header;
1606 struct lprocfs_counter *percpu_cntr;
1607 unsigned long flags = 0;
1609 unsigned int num_cpu;
1611 LASSERT(stats != NULL);
1613 header = &stats->ls_cnt_header[index];
1614 LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1615 index, name, units);
1617 header->lc_config = conf;
1618 header->lc_name = name;
1619 header->lc_units = units;
1621 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1622 for (i = 0; i < num_cpu; ++i) {
1623 if (!stats->ls_percpu[i])
1625 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1626 percpu_cntr->lc_count = 0;
1627 percpu_cntr->lc_min = LC_MIN_INIT;
1628 percpu_cntr->lc_max = 0;
1629 percpu_cntr->lc_sumsquare = 0;
1630 percpu_cntr->lc_sum = 0;
1631 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1632 percpu_cntr->lc_sum_irq = 0;
1634 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1636 EXPORT_SYMBOL(lprocfs_counter_init);
1638 static const char * const mps_stats[] = {
1639 [LPROC_MD_CLOSE] = "close",
1640 [LPROC_MD_CREATE] = "create",
1641 [LPROC_MD_ENQUEUE] = "enqueue",
1642 [LPROC_MD_GETATTR] = "getattr",
1643 [LPROC_MD_INTENT_LOCK] = "intent_lock",
1644 [LPROC_MD_LINK] = "link",
1645 [LPROC_MD_RENAME] = "rename",
1646 [LPROC_MD_SETATTR] = "setattr",
1647 [LPROC_MD_FSYNC] = "fsync",
1648 [LPROC_MD_READ_PAGE] = "read_page",
1649 [LPROC_MD_UNLINK] = "unlink",
1650 [LPROC_MD_SETXATTR] = "setxattr",
1651 [LPROC_MD_GETXATTR] = "getxattr",
1652 [LPROC_MD_INTENT_GETATTR_ASYNC] = "intent_getattr_async",
1653 [LPROC_MD_REVALIDATE_LOCK] = "revalidate_lock",
1656 int lprocfs_alloc_md_stats(struct obd_device *obd,
1657 unsigned int num_private_stats)
1659 struct lprocfs_stats *stats;
1660 unsigned int num_stats;
1664 * TODO Ensure that this function is only used where
1665 * appropriate by adding an assertion to the effect that
1666 * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1667 * because mdt_procfs_init() uses this function to allocate
1668 * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1669 * mdt layer does not use the md_ops interface. This is
1670 * confusing and a waste of memory. See LU-2484.
1672 LASSERT(obd->obd_proc_entry != NULL);
1673 LASSERT(obd->obd_md_stats == NULL);
1675 num_stats = ARRAY_SIZE(mps_stats) + num_private_stats;
1676 stats = lprocfs_alloc_stats(num_stats, 0);
1680 for (i = 0; i < ARRAY_SIZE(mps_stats); i++) {
1681 lprocfs_counter_init(stats, i, 0, mps_stats[i], "reqs");
1682 if (!stats->ls_cnt_header[i].lc_name) {
1683 CERROR("Missing md_stat initializer md_op operation at offset %d. Aborting.\n",
1689 rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1691 lprocfs_free_stats(&stats);
1693 obd->obd_md_stats = stats;
1698 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1700 void lprocfs_free_md_stats(struct obd_device *obd)
1702 struct lprocfs_stats *stats = obd->obd_md_stats;
1705 obd->obd_md_stats = NULL;
1706 lprocfs_free_stats(&stats);
1709 EXPORT_SYMBOL(lprocfs_free_md_stats);
1711 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1713 lprocfs_counter_init(ldlm_stats,
1714 LDLM_ENQUEUE - LDLM_FIRST_OPC,
1715 0, "ldlm_enqueue", "reqs");
1716 lprocfs_counter_init(ldlm_stats,
1717 LDLM_CONVERT - LDLM_FIRST_OPC,
1718 0, "ldlm_convert", "reqs");
1719 lprocfs_counter_init(ldlm_stats,
1720 LDLM_CANCEL - LDLM_FIRST_OPC,
1721 0, "ldlm_cancel", "reqs");
1722 lprocfs_counter_init(ldlm_stats,
1723 LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1724 0, "ldlm_bl_callback", "reqs");
1725 lprocfs_counter_init(ldlm_stats,
1726 LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1727 0, "ldlm_cp_callback", "reqs");
1728 lprocfs_counter_init(ldlm_stats,
1729 LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1730 0, "ldlm_gl_callback", "reqs");
1732 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1734 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1735 struct lprocfs_counter_header *header,
1736 enum lprocfs_stats_flags flags,
1737 enum lprocfs_fields_flags field)
1745 case LPROCFS_FIELDS_FLAGS_CONFIG:
1746 ret = header->lc_config;
1748 case LPROCFS_FIELDS_FLAGS_SUM:
1750 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1751 ret += lc->lc_sum_irq;
1753 case LPROCFS_FIELDS_FLAGS_MIN:
1756 case LPROCFS_FIELDS_FLAGS_MAX:
1759 case LPROCFS_FIELDS_FLAGS_AVG:
1760 ret = (lc->lc_max - lc->lc_min) / 2;
1762 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1763 ret = lc->lc_sumsquare;
1765 case LPROCFS_FIELDS_FLAGS_COUNT:
1773 EXPORT_SYMBOL(lprocfs_read_helper);
1775 /* Obtains the conversion factor for the unit specified */
1776 static int get_mult(char unit, __u64 *mult)
1781 /* 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 int lu_str_to_s64(char *buffer, unsigned long count, __s64 *val, char defunit)
1996 unsigned int offset = 0;
1997 int signed sign = 1;
1998 __u64 max = LLONG_MAX;
2001 if (defunit != '1') {
2002 rc = get_mult(defunit, &mult);
2007 /* keep track of our sign */
2008 if (*buffer == '-') {
2011 /* equivalent to max = -LLONG_MIN, avoids overflow */
2015 rc = str_to_u64_parse(buffer + offset, count - offset,
2020 /* check for overflow/underflow */
2024 *val = (__s64)tmp * sign;
2028 EXPORT_SYMBOL(lu_str_to_s64);
2030 /* identical to s64 version, but does not handle overflow */
2031 static int str_to_u64_internal(const char __user *buffer, unsigned long count,
2032 __u64 *val, __u64 def_mult, bool allow_units)
2035 unsigned int offset = 0;
2038 if (count > (sizeof(kernbuf) - 1))
2041 if (copy_from_user(kernbuf, buffer, count))
2044 kernbuf[count] = '\0';
2046 rc = str_to_u64_parse(kernbuf + offset, count - offset,
2047 val, def_mult, allow_units);
2054 * Convert a user string into a signed 64 bit number. This function produces
2055 * an error when the value parsed from the string times multiplier underflows or
2056 * overflows. This function only accepts strings that contains digits, an
2057 * optional decimal, and a char representing a unit at the end. If a unit is
2058 * specified in the string, the multiplier provided by the caller is ignored.
2059 * This function can also accept hexadecimal strings which are prefixed with
2062 * \param[in] buffer string consisting of numbers, a decimal, and a unit
2063 * \param[in] count buffer length
2064 * \param[in] val if successful, the value represented by the string
2065 * \param[in] defunit default unit if string doesn't contain one
2067 * \retval 0 on success
2068 * \retval negative number on error
2070 int lprocfs_str_with_units_to_s64(const char __user *buffer,
2071 unsigned long count, __s64 *val, char defunit)
2075 if (count > (sizeof(kernbuf) - 1))
2078 if (copy_from_user(kernbuf, buffer, count))
2081 kernbuf[count] = '\0';
2083 return lu_str_to_s64(kernbuf, count, val, defunit);
2085 EXPORT_SYMBOL(lprocfs_str_with_units_to_s64);
2087 /* identical to s64 version above, but does not handle overflow */
2088 int lprocfs_str_with_units_to_u64(const char __user *buffer,
2089 unsigned long count, __u64 *val, char defunit)
2094 if (defunit != '1') {
2095 rc = get_mult(defunit, &mult);
2100 return str_to_u64_internal(buffer, count, val, mult, true);
2102 EXPORT_SYMBOL(lprocfs_str_with_units_to_u64);
2104 char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
2113 if (!memcmp(s1, s2, l2))
2119 EXPORT_SYMBOL(lprocfs_strnstr);
2122 * Find the string \a name in the input \a buffer, and return a pointer to the
2123 * value immediately following \a name, reducing \a count appropriately.
2124 * If \a name is not found the original \a buffer is returned.
2126 char *lprocfs_find_named_value(const char *buffer, const char *name,
2130 size_t buflen = *count;
2132 /* there is no strnstr() in rhel5 and ubuntu kernels */
2133 val = lprocfs_strnstr(buffer, name, buflen);
2135 return (char *)buffer;
2137 val += strlen(name); /* skip prefix */
2138 while (val < buffer + buflen && isspace(*val)) /* skip separator */
2142 while (val < buffer + buflen && isalnum(*val)) {
2147 return val - *count;
2149 EXPORT_SYMBOL(lprocfs_find_named_value);
2151 int ldebugfs_seq_create(struct dentry *parent, const char *name, umode_t mode,
2152 const struct file_operations *seq_fops, void *data)
2154 struct dentry *entry;
2156 /* Disallow secretly (un)writable entries. */
2157 LASSERT((!seq_fops->write) == (!(mode & 0222)));
2159 entry = debugfs_create_file(name, mode, parent, data, seq_fops);
2160 if (IS_ERR_OR_NULL(entry))
2161 return entry ? PTR_ERR(entry) : -ENOMEM;
2165 EXPORT_SYMBOL_GPL(ldebugfs_seq_create);
2167 int lprocfs_seq_create(struct proc_dir_entry *parent,
2170 const struct file_operations *seq_fops,
2173 struct proc_dir_entry *entry;
2176 /* Disallow secretly (un)writable entries. */
2177 LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
2179 entry = proc_create_data(name, mode, parent, seq_fops, data);
2186 EXPORT_SYMBOL(lprocfs_seq_create);
2188 int lprocfs_obd_seq_create(struct obd_device *dev,
2191 const struct file_operations *seq_fops,
2194 return (lprocfs_seq_create(dev->obd_proc_entry, name,
2195 mode, seq_fops, data));
2197 EXPORT_SYMBOL(lprocfs_obd_seq_create);
2199 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
2201 if (value >= OBD_HIST_MAX)
2202 value = OBD_HIST_MAX - 1;
2204 spin_lock(&oh->oh_lock);
2205 oh->oh_buckets[value]++;
2206 spin_unlock(&oh->oh_lock);
2208 EXPORT_SYMBOL(lprocfs_oh_tally);
2210 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
2212 unsigned int val = 0;
2214 if (likely(value != 0))
2215 val = min(fls(value - 1), OBD_HIST_MAX);
2217 lprocfs_oh_tally(oh, val);
2219 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
2221 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
2223 unsigned long ret = 0;
2226 for (i = 0; i < OBD_HIST_MAX; i++)
2227 ret += oh->oh_buckets[i];
2230 EXPORT_SYMBOL(lprocfs_oh_sum);
2232 void lprocfs_oh_clear(struct obd_histogram *oh)
2234 spin_lock(&oh->oh_lock);
2235 memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
2236 spin_unlock(&oh->oh_lock);
2238 EXPORT_SYMBOL(lprocfs_oh_clear);
2240 ssize_t lustre_attr_show(struct kobject *kobj,
2241 struct attribute *attr, char *buf)
2243 struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
2245 return a->show ? a->show(kobj, attr, buf) : 0;
2247 EXPORT_SYMBOL_GPL(lustre_attr_show);
2249 ssize_t lustre_attr_store(struct kobject *kobj, struct attribute *attr,
2250 const char *buf, size_t len)
2252 struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
2254 return a->store ? a->store(kobj, attr, buf, len) : len;
2256 EXPORT_SYMBOL_GPL(lustre_attr_store);
2258 const struct sysfs_ops lustre_sysfs_ops = {
2259 .show = lustre_attr_show,
2260 .store = lustre_attr_store,
2262 EXPORT_SYMBOL_GPL(lustre_sysfs_ops);
2264 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
2266 struct obd_device *dev = data;
2267 struct client_obd *cli = &dev->u.cli;
2269 spin_lock(&cli->cl_loi_list_lock);
2270 seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
2271 spin_unlock(&cli->cl_loi_list_lock);
2274 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
2276 ssize_t lprocfs_obd_max_pages_per_rpc_seq_write(struct file *file,
2277 const char __user *buffer,
2278 size_t count, loff_t *off)
2280 struct obd_device *dev =
2281 ((struct seq_file *)file->private_data)->private;
2282 struct client_obd *cli = &dev->u.cli;
2283 struct obd_connect_data *ocd = &cli->cl_import->imp_connect_data;
2287 rc = lprocfs_str_with_units_to_s64(buffer, count, &val, '1');
2293 /* if the max_pages is specified in bytes, convert to pages */
2294 if (val >= ONE_MB_BRW_SIZE)
2297 LPROCFS_CLIMP_CHECK(dev);
2299 chunk_mask = ~((1 << (cli->cl_chunkbits - PAGE_SHIFT)) - 1);
2300 /* max_pages_per_rpc must be chunk aligned */
2301 val = (val + ~chunk_mask) & chunk_mask;
2302 if (val == 0 || (ocd->ocd_brw_size != 0 &&
2303 val > ocd->ocd_brw_size >> PAGE_SHIFT)) {
2304 LPROCFS_CLIMP_EXIT(dev);
2307 spin_lock(&cli->cl_loi_list_lock);
2308 cli->cl_max_pages_per_rpc = val;
2309 client_adjust_max_dirty(cli);
2310 spin_unlock(&cli->cl_loi_list_lock);
2312 LPROCFS_CLIMP_EXIT(dev);
2315 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_write);
2317 ssize_t short_io_bytes_show(struct kobject *kobj, struct attribute *attr,
2320 struct obd_device *dev = container_of(kobj, struct obd_device,
2322 struct client_obd *cli = &dev->u.cli;
2325 spin_lock(&cli->cl_loi_list_lock);
2326 rc = sprintf(buf, "%d\n", cli->cl_max_short_io_bytes);
2327 spin_unlock(&cli->cl_loi_list_lock);
2330 EXPORT_SYMBOL(short_io_bytes_show);
2332 /* Used to catch people who think they're specifying pages. */
2333 #define MIN_SHORT_IO_BYTES 64U
2335 ssize_t short_io_bytes_store(struct kobject *kobj, struct attribute *attr,
2336 const char *buffer, size_t count)
2338 struct obd_device *dev = container_of(kobj, struct obd_device,
2340 struct client_obd *cli = &dev->u.cli;
2344 LPROCFS_CLIMP_CHECK(dev);
2346 rc = kstrtouint(buffer, 0, &val);
2350 if (val && (val < MIN_SHORT_IO_BYTES || val > OBD_MAX_SHORT_IO_BYTES))
2351 GOTO(out, rc = -ERANGE);
2355 spin_lock(&cli->cl_loi_list_lock);
2356 if (val > (cli->cl_max_pages_per_rpc << PAGE_SHIFT))
2359 cli->cl_max_short_io_bytes = val;
2360 spin_unlock(&cli->cl_loi_list_lock);
2363 LPROCFS_CLIMP_EXIT(dev);
2366 EXPORT_SYMBOL(short_io_bytes_store);
2368 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2369 struct root_squash_info *squash, char *name)
2372 char kernbuf[64], *tmp, *errmsg;
2373 unsigned long uid, gid;
2376 if (count >= sizeof(kernbuf)) {
2377 errmsg = "string too long";
2378 GOTO(failed_noprint, rc = -EINVAL);
2380 if (copy_from_user(kernbuf, buffer, count)) {
2381 errmsg = "bad address";
2382 GOTO(failed_noprint, rc = -EFAULT);
2384 kernbuf[count] = '\0';
2386 /* look for uid gid separator */
2387 tmp = strchr(kernbuf, ':');
2389 errmsg = "needs uid:gid format";
2390 GOTO(failed, rc = -EINVAL);
2396 if (kstrtoul(kernbuf, 0, &uid) != 0) {
2398 GOTO(failed, rc = -EINVAL);
2402 if (kstrtoul(tmp, 0, &gid) != 0) {
2404 GOTO(failed, rc = -EINVAL);
2407 squash->rsi_uid = uid;
2408 squash->rsi_gid = gid;
2410 LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2411 name, squash->rsi_uid, squash->rsi_gid);
2419 CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2420 name, kernbuf, errmsg, rc);
2423 CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2427 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2430 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2431 struct root_squash_info *squash, char *name)
2434 char *kernbuf = NULL;
2436 struct list_head tmp;
2441 errmsg = "string too long";
2442 GOTO(failed, rc = -EINVAL);
2445 OBD_ALLOC(kernbuf, count + 1);
2447 errmsg = "no memory";
2448 GOTO(failed, rc = -ENOMEM);
2450 if (copy_from_user(kernbuf, buffer, count)) {
2451 errmsg = "bad address";
2452 GOTO(failed, rc = -EFAULT);
2454 kernbuf[count] = '\0';
2456 if (count > 0 && kernbuf[count - 1] == '\n')
2459 if ((len == 4 && strncmp(kernbuf, "NONE", len) == 0) ||
2460 (len == 5 && strncmp(kernbuf, "clear", len) == 0)) {
2461 /* empty string is special case */
2462 spin_lock(&squash->rsi_lock);
2463 if (!list_empty(&squash->rsi_nosquash_nids))
2464 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2465 spin_unlock(&squash->rsi_lock);
2466 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2467 OBD_FREE(kernbuf, count + 1);
2471 INIT_LIST_HEAD(&tmp);
2472 if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2473 errmsg = "can't parse";
2474 GOTO(failed, rc = -EINVAL);
2476 LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2478 OBD_FREE(kernbuf, count + 1);
2481 spin_lock(&squash->rsi_lock);
2482 if (!list_empty(&squash->rsi_nosquash_nids))
2483 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2484 list_splice(&tmp, &squash->rsi_nosquash_nids);
2485 spin_unlock(&squash->rsi_lock);
2491 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2492 name, kernbuf, errmsg, rc);
2493 OBD_FREE(kernbuf, count + 1);
2495 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2500 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2502 #endif /* CONFIG_PROC_FS*/