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 == NULL || name == NULL || fops == NULL)
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, const char *format, ...)
112 struct proc_dir_entry *entry;
116 if (parent == NULL || format == NULL)
119 OBD_ALLOC_WAIT(dest, MAX_STRING_SIZE + 1);
123 va_start(ap, format);
124 vsnprintf(dest, MAX_STRING_SIZE, format, ap);
127 entry = proc_symlink(name, parent, dest);
129 CERROR("LprocFS: Could not create symbolic link from "
130 "%s to %s\n", name, dest);
132 OBD_FREE(dest, MAX_STRING_SIZE + 1);
135 EXPORT_SYMBOL(lprocfs_add_symlink);
137 static const struct file_operations lprocfs_generic_fops = { };
139 int ldebugfs_add_vars(struct dentry *parent, struct lprocfs_vars *list,
142 if (IS_ERR_OR_NULL(parent) || IS_ERR_OR_NULL(list))
146 struct dentry *entry;
149 if (list->proc_mode != 0000) {
150 mode = list->proc_mode;
151 } else if (list->fops) {
152 if (list->fops->read)
154 if (list->fops->write)
157 entry = debugfs_create_file(list->name, mode, parent,
159 list->fops ? : &lprocfs_generic_fops);
160 if (IS_ERR_OR_NULL(entry))
161 return entry ? PTR_ERR(entry) : -ENOMEM;
166 EXPORT_SYMBOL_GPL(ldebugfs_add_vars);
171 * \param root [in] The parent proc entry on which new entry will be added.
172 * \param list [in] Array of proc entries to be added.
173 * \param data [in] The argument to be passed when entries read/write routines
174 * are called through /proc file.
176 * \retval 0 on success
180 lprocfs_add_vars(struct proc_dir_entry *root, struct lprocfs_vars *list,
183 if (root == NULL || list == NULL)
186 while (list->name != NULL) {
187 struct proc_dir_entry *proc;
190 if (list->proc_mode != 0000) {
191 mode = list->proc_mode;
192 } else if (list->fops) {
193 if (list->fops->read)
195 if (list->fops->write)
198 proc = proc_create_data(list->name, mode, root,
199 list->fops ?: &lprocfs_generic_fops,
207 EXPORT_SYMBOL(lprocfs_add_vars);
209 void ldebugfs_remove(struct dentry **entryp)
211 debugfs_remove_recursive(*entryp);
214 EXPORT_SYMBOL_GPL(ldebugfs_remove);
216 #ifndef HAVE_REMOVE_PROC_SUBTREE
217 /* for b=10866, global variable */
218 DECLARE_RWSEM(_lprocfs_lock);
219 EXPORT_SYMBOL(_lprocfs_lock);
221 static void lprocfs_remove_nolock(struct proc_dir_entry **proot)
223 struct proc_dir_entry *root = *proot;
224 struct proc_dir_entry *temp = root;
225 struct proc_dir_entry *rm_entry;
226 struct proc_dir_entry *parent;
229 if (root == NULL || IS_ERR(root))
232 parent = root->parent;
233 LASSERT(parent != NULL);
236 while (temp->subdir != NULL)
242 /* Memory corruption once caused this to fail, and
243 without this LASSERT we would loop here forever. */
244 LASSERTF(strlen(rm_entry->name) == rm_entry->namelen,
245 "0x%p %s/%s len %d\n", rm_entry, temp->name,
246 rm_entry->name, (int)strlen(rm_entry->name));
248 remove_proc_entry(rm_entry->name, temp);
254 int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
256 struct proc_dir_entry *t = NULL;
257 struct proc_dir_entry **p;
260 LASSERT(parent != NULL);
263 down_write(&_lprocfs_lock);
264 /* lookup target name */
265 for (p = &parent->subdir; *p; p = &(*p)->next) {
266 if ((*p)->namelen != len)
268 if (memcmp(name, (*p)->name, len))
275 /* verify it's empty: do not count "num_refs" */
276 for (p = &t->subdir; *p; p = &(*p)->next) {
277 if ((*p)->namelen != strlen("num_refs")) {
281 if (memcmp("num_refs", (*p)->name,
282 strlen("num_refs"))) {
290 lprocfs_remove_nolock(&t);
292 up_write(&_lprocfs_lock);
295 #endif /* !HAVE_REMOVE_PROC_SUBTREE */
297 #ifndef HAVE_PROC_REMOVE
298 void proc_remove(struct proc_dir_entry *de)
300 #ifndef HAVE_REMOVE_PROC_SUBTREE
301 down_write(&_lprocfs_lock); /* search vs remove race */
302 lprocfs_remove_nolock(&de);
303 up_write(&_lprocfs_lock);
306 remove_proc_subtree(de->name, de->parent);
311 void lprocfs_remove(struct proc_dir_entry **rooth)
316 EXPORT_SYMBOL(lprocfs_remove);
318 void lprocfs_remove_proc_entry(const char *name, struct proc_dir_entry *parent)
320 LASSERT(parent != NULL);
321 remove_proc_entry(name, parent);
323 EXPORT_SYMBOL(lprocfs_remove_proc_entry);
325 struct dentry *ldebugfs_register(const char *name, struct dentry *parent,
326 struct lprocfs_vars *list, void *data)
328 struct dentry *entry;
330 entry = debugfs_create_dir(name, parent);
331 if (IS_ERR_OR_NULL(entry)) {
332 entry = entry ?: ERR_PTR(-ENOMEM);
336 if (!IS_ERR_OR_NULL(list)) {
339 rc = ldebugfs_add_vars(entry, list, data);
341 debugfs_remove(entry);
348 EXPORT_SYMBOL_GPL(ldebugfs_register);
350 struct proc_dir_entry *
351 lprocfs_register(const char *name, struct proc_dir_entry *parent,
352 struct lprocfs_vars *list, void *data)
354 struct proc_dir_entry *newchild;
356 newchild = proc_mkdir(name, parent);
357 if (newchild == NULL)
358 return ERR_PTR(-ENOMEM);
361 int rc = lprocfs_add_vars(newchild, list, data);
363 lprocfs_remove(&newchild);
369 EXPORT_SYMBOL(lprocfs_register);
371 /* Generic callbacks */
372 int lprocfs_uuid_seq_show(struct seq_file *m, void *data)
374 struct obd_device *obd = data;
376 LASSERT(obd != NULL);
377 seq_printf(m, "%s\n", obd->obd_uuid.uuid);
380 EXPORT_SYMBOL(lprocfs_uuid_seq_show);
382 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
385 struct obd_device *obd = container_of(kobj, struct obd_device,
388 return sprintf(buf, "%s\n", obd->obd_uuid.uuid);
390 LUSTRE_RO_ATTR(uuid);
392 static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
395 struct obd_device *obd = container_of(kobj, struct obd_device,
397 struct obd_statfs osfs;
400 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
401 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
404 return sprintf(buf, "%u\n", osfs.os_bsize);
408 LUSTRE_RO_ATTR(blocksize);
410 static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
413 struct obd_device *obd = container_of(kobj, struct obd_device,
415 struct obd_statfs osfs;
418 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
419 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
422 u32 blk_size = osfs.os_bsize >> 10;
423 u64 result = osfs.os_blocks;
425 while (blk_size >>= 1)
428 return sprintf(buf, "%llu\n", result);
433 LUSTRE_RO_ATTR(kbytestotal);
435 static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
438 struct obd_device *obd = container_of(kobj, struct obd_device,
440 struct obd_statfs osfs;
443 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
444 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
447 u32 blk_size = osfs.os_bsize >> 10;
448 u64 result = osfs.os_bfree;
450 while (blk_size >>= 1)
453 return sprintf(buf, "%llu\n", result);
458 LUSTRE_RO_ATTR(kbytesfree);
460 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
463 struct obd_device *obd = container_of(kobj, struct obd_device,
465 struct obd_statfs osfs;
468 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
469 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
472 u32 blk_size = osfs.os_bsize >> 10;
473 u64 result = osfs.os_bavail;
475 while (blk_size >>= 1)
478 return sprintf(buf, "%llu\n", result);
483 LUSTRE_RO_ATTR(kbytesavail);
485 static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
488 struct obd_device *obd = container_of(kobj, struct obd_device,
490 struct obd_statfs osfs;
493 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
494 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
497 return sprintf(buf, "%llu\n", osfs.os_files);
501 LUSTRE_RO_ATTR(filestotal);
503 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
506 struct obd_device *obd = container_of(kobj, struct obd_device,
508 struct obd_statfs osfs;
511 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
512 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
515 return sprintf(buf, "%llu\n", osfs.os_ffree);
519 LUSTRE_RO_ATTR(filesfree);
521 ssize_t conn_uuid_show(struct kobject *kobj, struct attribute *attr, char *buf)
523 struct obd_device *obd = container_of(kobj, struct obd_device,
525 struct ptlrpc_connection *conn;
528 LPROCFS_CLIMP_CHECK(obd);
529 conn = obd->u.cli.cl_import->imp_connection;
530 if (conn && obd->u.cli.cl_import)
531 count = sprintf(buf, "%s\n", conn->c_remote_uuid.uuid);
533 count = sprintf(buf, "%s\n", "<none>");
535 LPROCFS_CLIMP_EXIT(obd);
538 EXPORT_SYMBOL(conn_uuid_show);
540 int lprocfs_server_uuid_seq_show(struct seq_file *m, void *data)
542 struct obd_device *obd = data;
543 struct obd_import *imp;
544 char *imp_state_name = NULL;
547 LASSERT(obd != NULL);
548 LPROCFS_CLIMP_CHECK(obd);
549 imp = obd->u.cli.cl_import;
550 imp_state_name = ptlrpc_import_state_name(imp->imp_state);
551 seq_printf(m, "%s\t%s%s\n", obd2cli_tgt(obd), imp_state_name,
552 imp->imp_deactive ? "\tDEACTIVATED" : "");
554 LPROCFS_CLIMP_EXIT(obd);
557 EXPORT_SYMBOL(lprocfs_server_uuid_seq_show);
559 /** add up per-cpu counters */
562 * Lock statistics structure for access, possibly only on this CPU.
564 * The statistics struct may be allocated with per-CPU structures for
565 * efficient concurrent update (usually only on server-wide stats), or
566 * as a single global struct (e.g. for per-client or per-job statistics),
567 * so the required locking depends on the type of structure allocated.
569 * For per-CPU statistics, pin the thread to the current cpuid so that
570 * will only access the statistics for that CPU. If the stats structure
571 * for the current CPU has not been allocated (or previously freed),
572 * allocate it now. The per-CPU statistics do not need locking since
573 * the thread is pinned to the CPU during update.
575 * For global statistics, lock the stats structure to prevent concurrent update.
577 * \param[in] stats statistics structure to lock
578 * \param[in] opc type of operation:
579 * LPROCFS_GET_SMP_ID: "lock" and return current CPU index
580 * for incrementing statistics for that CPU
581 * LPROCFS_GET_NUM_CPU: "lock" and return number of used
582 * CPU indices to iterate over all indices
583 * \param[out] flags CPU interrupt saved state for IRQ-safe locking
585 * \retval cpuid of current thread or number of allocated structs
586 * \retval negative on error (only for opc LPROCFS_GET_SMP_ID + per-CPU stats)
588 int lprocfs_stats_lock(struct lprocfs_stats *stats,
589 enum lprocfs_stats_lock_ops opc,
590 unsigned long *flags)
592 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
593 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
594 spin_lock_irqsave(&stats->ls_lock, *flags);
596 spin_lock(&stats->ls_lock);
597 return opc == LPROCFS_GET_NUM_CPU ? 1 : 0;
601 case LPROCFS_GET_SMP_ID: {
602 unsigned int cpuid = get_cpu();
604 if (unlikely(!stats->ls_percpu[cpuid])) {
605 int rc = lprocfs_stats_alloc_one(stats, cpuid);
614 case LPROCFS_GET_NUM_CPU:
615 return stats->ls_biggest_alloc_num;
622 * Unlock statistics structure after access.
624 * Unlock the lock acquired via lprocfs_stats_lock() for global statistics,
625 * or unpin this thread from the current cpuid for per-CPU statistics.
627 * This function must be called using the same arguments as used when calling
628 * lprocfs_stats_lock() so that the correct operation can be performed.
630 * \param[in] stats statistics structure to unlock
631 * \param[in] opc type of operation (current cpuid or number of structs)
632 * \param[in] flags CPU interrupt saved state for IRQ-safe locking
634 void lprocfs_stats_unlock(struct lprocfs_stats *stats,
635 enum lprocfs_stats_lock_ops opc,
636 unsigned long *flags)
638 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
639 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
640 spin_unlock_irqrestore(&stats->ls_lock, *flags);
642 spin_unlock(&stats->ls_lock);
643 } else if (opc == LPROCFS_GET_SMP_ID) {
648 /** add up per-cpu counters */
649 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
650 struct lprocfs_counter *cnt)
652 unsigned int num_entry;
653 struct lprocfs_counter *percpu_cntr;
655 unsigned long flags = 0;
657 memset(cnt, 0, sizeof(*cnt));
660 /* set count to 1 to avoid divide-by-zero errs in callers */
665 cnt->lc_min = LC_MIN_INIT;
667 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
669 for (i = 0; i < num_entry; i++) {
670 if (stats->ls_percpu[i] == NULL)
672 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
674 cnt->lc_count += percpu_cntr->lc_count;
675 cnt->lc_sum += percpu_cntr->lc_sum;
676 if (percpu_cntr->lc_min < cnt->lc_min)
677 cnt->lc_min = percpu_cntr->lc_min;
678 if (percpu_cntr->lc_max > cnt->lc_max)
679 cnt->lc_max = percpu_cntr->lc_max;
680 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
683 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
686 static void obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
690 if (imp->imp_obd->obd_no_recov) {
691 seq_printf(m, "no_recov");
695 flag2str(imp, invalid);
696 flag2str(imp, deactive);
697 flag2str(imp, replayable);
698 flag2str(imp, delayed_recovery);
699 flag2str(imp, vbr_failed);
700 flag2str(imp, pingable);
701 flag2str(imp, resend_replay);
702 flag2str(imp, no_pinger_recover);
703 flag2str(imp, connect_tried);
706 static const char *obd_connect_names[] = {
725 "remote_client_by_force",
734 "mds_mds_connection",
737 "alt_checksum_algorithm",
773 "file_secctx", /* 0x01 */
774 "lockaheadv2", /* 0x02 */
775 "dir_migrate", /* 0x04 */
776 "unknown", /* 0x08 */
777 "unknown", /* 0x10 */
780 "lock_convert", /* 0x80 */
781 "archive_id_array", /* 0x100 */
785 void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, __u64 flags2,
792 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
794 seq_printf(m, "%s%s",
795 first ? "" : sep, obd_connect_names[i]);
800 if (flags & ~(mask - 1)) {
801 seq_printf(m, "%sunknown_%#llx",
802 first ? "" : sep, flags & ~(mask - 1));
806 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
809 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
811 seq_printf(m, "%s%s",
812 first ? "" : sep, obd_connect_names[i]);
817 if (flags2 & ~(mask - 1)) {
818 seq_printf(m, "%sunknown2_%#llx",
819 first ? "" : sep, flags2 & ~(mask - 1));
823 EXPORT_SYMBOL(obd_connect_seq_flags2str);
825 int obd_connect_flags2str(char *page, int count, __u64 flags, __u64 flags2,
831 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
833 ret += snprintf(page + ret, count - ret, "%s%s",
834 ret ? sep : "", obd_connect_names[i]);
837 if (flags & ~(mask - 1))
838 ret += snprintf(page + ret, count - ret,
840 ret ? sep : "", flags & ~(mask - 1));
842 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
845 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
847 ret += snprintf(page + ret, count - ret, "%s%s",
848 ret ? sep : "", obd_connect_names[i]);
851 if (flags2 & ~(mask - 1))
852 ret += snprintf(page + ret, count - ret,
854 ret ? sep : "", flags2 & ~(mask - 1));
858 EXPORT_SYMBOL(obd_connect_flags2str);
861 obd_connect_data_seqprint(struct seq_file *m, struct obd_connect_data *ocd)
865 LASSERT(ocd != NULL);
866 flags = ocd->ocd_connect_flags;
868 seq_printf(m, " connect_data:\n"
871 ocd->ocd_connect_flags,
873 if (flags & OBD_CONNECT_VERSION)
874 seq_printf(m, " target_version: %u.%u.%u.%u\n",
875 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
876 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
877 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
878 OBD_OCD_VERSION_FIX(ocd->ocd_version));
879 if (flags & OBD_CONNECT_MDS)
880 seq_printf(m, " mdt_index: %d\n", ocd->ocd_group);
881 if (flags & OBD_CONNECT_GRANT)
882 seq_printf(m, " initial_grant: %d\n", ocd->ocd_grant);
883 if (flags & OBD_CONNECT_INDEX)
884 seq_printf(m, " target_index: %u\n", ocd->ocd_index);
885 if (flags & OBD_CONNECT_BRW_SIZE)
886 seq_printf(m, " max_brw_size: %d\n", ocd->ocd_brw_size);
887 if (flags & OBD_CONNECT_IBITS)
888 seq_printf(m, " ibits_known: %#llx\n",
889 ocd->ocd_ibits_known);
890 if (flags & OBD_CONNECT_GRANT_PARAM)
891 seq_printf(m, " grant_block_size: %d\n"
892 " grant_inode_size: %d\n"
893 " grant_max_extent_size: %d\n"
894 " grant_extent_tax: %d\n",
895 1 << ocd->ocd_grant_blkbits,
896 1 << ocd->ocd_grant_inobits,
897 ocd->ocd_grant_max_blks << ocd->ocd_grant_blkbits,
898 ocd->ocd_grant_tax_kb << 10);
899 if (flags & OBD_CONNECT_TRANSNO)
900 seq_printf(m, " first_transno: %#llx\n",
902 if (flags & OBD_CONNECT_CKSUM)
903 seq_printf(m, " cksum_types: %#x\n",
904 ocd->ocd_cksum_types);
905 if (flags & OBD_CONNECT_MAX_EASIZE)
906 seq_printf(m, " max_easize: %d\n", ocd->ocd_max_easize);
907 if (flags & OBD_CONNECT_MAXBYTES)
908 seq_printf(m, " max_object_bytes: %llu\n",
910 if (flags & OBD_CONNECT_MULTIMODRPCS)
911 seq_printf(m, " max_mod_rpcs: %hu\n",
912 ocd->ocd_maxmodrpcs);
915 int lprocfs_import_seq_show(struct seq_file *m, void *data)
917 char nidstr[LNET_NIDSTR_SIZE];
918 struct lprocfs_counter ret;
919 struct lprocfs_counter_header *header;
920 struct obd_device *obd = (struct obd_device *)data;
921 struct obd_import *imp;
922 struct obd_import_conn *conn;
923 struct obd_connect_data *ocd;
928 LASSERT(obd != NULL);
929 LPROCFS_CLIMP_CHECK(obd);
930 imp = obd->u.cli.cl_import;
931 ocd = &imp->imp_connect_data;
933 seq_printf(m, "import:\n"
937 " connect_flags: [ ",
940 ptlrpc_import_state_name(imp->imp_state));
941 obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
942 imp->imp_connect_data.ocd_connect_flags2,
944 seq_printf(m, " ]\n");
945 obd_connect_data_seqprint(m, ocd);
946 seq_printf(m, " import_flags: [ ");
947 obd_import_flags2str(imp, m);
951 " failover_nids: [ ");
952 spin_lock(&imp->imp_lock);
954 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
955 libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
956 nidstr, sizeof(nidstr));
957 seq_printf(m, "%s%s", j ? ", " : "", nidstr);
960 if (imp->imp_connection != NULL)
961 libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
962 nidstr, sizeof(nidstr));
964 strncpy(nidstr, "<none>", sizeof(nidstr));
966 " current_connection: %s\n"
967 " connection_attempts: %u\n"
969 " in-progress_invalidations: %u\n"
974 atomic_read(&imp->imp_inval_count),
975 ktime_get_real_seconds() - imp->imp_last_reply_time);
976 spin_unlock(&imp->imp_lock);
978 if (obd->obd_svc_stats == NULL)
981 header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
982 lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
983 if (ret.lc_count != 0) {
984 /* first argument to do_div MUST be __u64 */
985 __u64 sum = ret.lc_sum;
986 do_div(sum, ret.lc_count);
990 seq_printf(m, " rpcs:\n"
992 " unregistering: %u\n"
994 " avg_waittime: %llu %s\n",
995 atomic_read(&imp->imp_inflight),
996 atomic_read(&imp->imp_unregistering),
997 atomic_read(&imp->imp_timeouts),
998 ret.lc_sum, header->lc_units);
1001 for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
1002 if (imp->imp_at.iat_portal[j] == 0)
1004 k = max_t(unsigned int, k,
1005 at_get(&imp->imp_at.iat_service_estimate[j]));
1007 seq_printf(m, " service_estimates:\n"
1008 " services: %u sec\n"
1009 " network: %u sec\n",
1011 at_get(&imp->imp_at.iat_net_latency));
1013 seq_printf(m, " transactions:\n"
1014 " last_replay: %llu\n"
1015 " peer_committed: %llu\n"
1016 " last_checked: %llu\n",
1017 imp->imp_last_replay_transno,
1018 imp->imp_peer_committed_transno,
1019 imp->imp_last_transno_checked);
1021 /* avg data rates */
1022 for (rw = 0; rw <= 1; rw++) {
1023 lprocfs_stats_collect(obd->obd_svc_stats,
1024 PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
1026 if (ret.lc_sum > 0 && ret.lc_count > 0) {
1027 /* first argument to do_div MUST be __u64 */
1028 __u64 sum = ret.lc_sum;
1029 do_div(sum, ret.lc_count);
1031 seq_printf(m, " %s_data_averages:\n"
1032 " bytes_per_rpc: %llu\n",
1033 rw ? "write" : "read",
1036 k = (int)ret.lc_sum;
1037 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
1038 header = &obd->obd_svc_stats->ls_cnt_header[j];
1039 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
1040 if (ret.lc_sum > 0 && ret.lc_count != 0) {
1041 /* first argument to do_div MUST be __u64 */
1042 __u64 sum = ret.lc_sum;
1043 do_div(sum, ret.lc_count);
1045 seq_printf(m, " %s_per_rpc: %llu\n",
1046 header->lc_units, ret.lc_sum);
1047 j = (int)ret.lc_sum;
1049 seq_printf(m, " MB_per_sec: %u.%.02u\n",
1050 k / j, (100 * k / j) % 100);
1055 LPROCFS_CLIMP_EXIT(obd);
1058 EXPORT_SYMBOL(lprocfs_import_seq_show);
1060 int lprocfs_state_seq_show(struct seq_file *m, void *data)
1062 struct obd_device *obd = (struct obd_device *)data;
1063 struct obd_import *imp;
1066 LASSERT(obd != NULL);
1067 LPROCFS_CLIMP_CHECK(obd);
1068 imp = obd->u.cli.cl_import;
1070 seq_printf(m, "current_state: %s\n",
1071 ptlrpc_import_state_name(imp->imp_state));
1072 seq_printf(m, "state_history:\n");
1073 k = imp->imp_state_hist_idx;
1074 for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
1075 struct import_state_hist *ish =
1076 &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
1077 if (ish->ish_state == 0)
1079 seq_printf(m, " - [ %lld, %s ]\n", (s64)ish->ish_time,
1080 ptlrpc_import_state_name(ish->ish_state));
1083 LPROCFS_CLIMP_EXIT(obd);
1086 EXPORT_SYMBOL(lprocfs_state_seq_show);
1088 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
1091 for (i = 0; i < AT_BINS; i++)
1092 seq_printf(m, "%3u ", at->at_hist[i]);
1093 seq_printf(m, "\n");
1096 EXPORT_SYMBOL(lprocfs_at_hist_helper);
1098 /* See also ptlrpc_lprocfs_timeouts_show_seq */
1099 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
1101 struct obd_device *obd = (struct obd_device *)data;
1102 struct obd_import *imp;
1103 unsigned int cur, worst;
1104 time64_t now, worstt;
1107 LASSERT(obd != NULL);
1108 LPROCFS_CLIMP_CHECK(obd);
1109 imp = obd->u.cli.cl_import;
1111 now = ktime_get_real_seconds();
1113 /* Some network health info for kicks */
1114 seq_printf(m, "%-10s : %lld, %llds ago\n",
1115 "last reply", (s64)imp->imp_last_reply_time,
1116 (s64)(now - imp->imp_last_reply_time));
1118 cur = at_get(&imp->imp_at.iat_net_latency);
1119 worst = imp->imp_at.iat_net_latency.at_worst_ever;
1120 worstt = imp->imp_at.iat_net_latency.at_worst_time;
1121 seq_printf(m, "%-10s : cur %3u worst %3u (at %lld, %llds ago) ",
1122 "network", cur, worst, (s64)worstt, (s64)(now - worstt));
1123 lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
1125 for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1126 if (imp->imp_at.iat_portal[i] == 0)
1128 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
1129 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
1130 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
1131 seq_printf(m, "portal %-2d : cur %3u worst %3u (at %lld, %llds ago) ",
1132 imp->imp_at.iat_portal[i], cur, worst, (s64)worstt,
1133 (s64)(now - worstt));
1134 lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
1137 LPROCFS_CLIMP_EXIT(obd);
1140 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
1142 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
1144 struct obd_device *obd = data;
1148 LPROCFS_CLIMP_CHECK(obd);
1149 flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
1150 flags2 = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags2;
1151 seq_printf(m, "flags=%#llx\n", flags);
1152 seq_printf(m, "flags2=%#llx\n", flags2);
1153 obd_connect_seq_flags2str(m, flags, flags2, "\n");
1154 seq_printf(m, "\n");
1155 LPROCFS_CLIMP_EXIT(obd);
1158 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
1160 static const struct attribute *obd_def_uuid_attrs[] = {
1161 &lustre_attr_uuid.attr,
1165 static const struct attribute *obd_def_attrs[] = {
1166 &lustre_attr_blocksize.attr,
1167 &lustre_attr_kbytestotal.attr,
1168 &lustre_attr_kbytesfree.attr,
1169 &lustre_attr_kbytesavail.attr,
1170 &lustre_attr_filestotal.attr,
1171 &lustre_attr_filesfree.attr,
1172 &lustre_attr_uuid.attr,
1176 static void obd_sysfs_release(struct kobject *kobj)
1178 struct obd_device *obd = container_of(kobj, struct obd_device,
1181 complete(&obd->obd_kobj_unregister);
1184 int lprocfs_obd_setup(struct obd_device *obd, bool uuid_only)
1186 struct lprocfs_vars *debugfs_vars = NULL;
1189 if (!obd || obd->obd_magic != OBD_DEVICE_MAGIC)
1192 rc = kobject_set_name(&obd->obd_kset.kobj, "%s", obd->obd_name);
1196 obd->obd_ktype.sysfs_ops = &lustre_sysfs_ops;
1197 obd->obd_ktype.release = obd_sysfs_release;
1199 obd->obd_kset.kobj.parent = obd->obd_type->typ_kobj;
1200 obd->obd_kset.kobj.ktype = &obd->obd_ktype;
1201 init_completion(&obd->obd_kobj_unregister);
1202 rc = kset_register(&obd->obd_kset);
1207 obd->obd_attrs = obd_def_uuid_attrs;
1209 obd->obd_attrs = obd_def_attrs;
1211 rc = sysfs_create_files(&obd->obd_kset.kobj, obd->obd_attrs);
1213 kset_unregister(&obd->obd_kset);
1217 if (!obd->obd_type->typ_procroot)
1218 debugfs_vars = obd->obd_vars;
1219 obd->obd_debugfs_entry = ldebugfs_register(obd->obd_name,
1220 obd->obd_type->typ_debugfs_entry,
1222 if (IS_ERR_OR_NULL(obd->obd_debugfs_entry)) {
1223 rc = obd->obd_debugfs_entry ? PTR_ERR(obd->obd_debugfs_entry)
1225 CERROR("error %d setting up debugfs for %s\n",
1227 obd->obd_debugfs_entry = NULL;
1229 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1230 obd->obd_attrs = NULL;
1231 kset_unregister(&obd->obd_kset);
1235 if (obd->obd_proc_entry || !obd->obd_type->typ_procroot)
1236 GOTO(already_registered, rc);
1238 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1239 obd->obd_type->typ_procroot,
1240 obd->obd_vars, obd);
1241 if (IS_ERR(obd->obd_proc_entry)) {
1242 rc = PTR_ERR(obd->obd_proc_entry);
1243 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1244 obd->obd_proc_entry = NULL;
1246 ldebugfs_remove(&obd->obd_debugfs_entry);
1247 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1248 obd->obd_attrs = NULL;
1249 kset_unregister(&obd->obd_kset);
1255 EXPORT_SYMBOL(lprocfs_obd_setup);
1257 int lprocfs_obd_cleanup(struct obd_device *obd)
1262 if (obd->obd_proc_exports_entry) {
1263 /* Should be no exports left */
1264 lprocfs_remove(&obd->obd_proc_exports_entry);
1265 obd->obd_proc_exports_entry = NULL;
1268 if (obd->obd_proc_entry) {
1269 lprocfs_remove(&obd->obd_proc_entry);
1270 obd->obd_proc_entry = NULL;
1273 if (!IS_ERR_OR_NULL(obd->obd_debugfs_entry))
1274 ldebugfs_remove(&obd->obd_debugfs_entry);
1276 /* obd device never allocated a kset */
1277 if (!obd->obd_kset.kobj.state_initialized)
1280 if (obd->obd_attrs) {
1281 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1282 obd->obd_attrs = NULL;
1285 kset_unregister(&obd->obd_kset);
1286 wait_for_completion(&obd->obd_kobj_unregister);
1289 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1291 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1293 struct lprocfs_counter *cntr;
1294 unsigned int percpusize;
1296 unsigned long flags = 0;
1299 LASSERT(stats->ls_percpu[cpuid] == NULL);
1300 LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1302 percpusize = lprocfs_stats_counter_size(stats);
1303 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1304 if (stats->ls_percpu[cpuid] != NULL) {
1306 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1307 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1308 spin_lock_irqsave(&stats->ls_lock, flags);
1310 spin_lock(&stats->ls_lock);
1311 if (stats->ls_biggest_alloc_num <= cpuid)
1312 stats->ls_biggest_alloc_num = cpuid + 1;
1313 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1314 spin_unlock_irqrestore(&stats->ls_lock, flags);
1316 spin_unlock(&stats->ls_lock);
1319 /* initialize the ls_percpu[cpuid] non-zero counter */
1320 for (i = 0; i < stats->ls_num; ++i) {
1321 cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1322 cntr->lc_min = LC_MIN_INIT;
1328 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1329 enum lprocfs_stats_flags flags)
1331 struct lprocfs_stats *stats;
1332 unsigned int num_entry;
1333 unsigned int percpusize = 0;
1339 if (lprocfs_no_percpu_stats != 0)
1340 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1342 if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1345 num_entry = num_possible_cpus();
1347 /* alloc percpu pointers for all possible cpu slots */
1348 LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1352 stats->ls_num = num;
1353 stats->ls_flags = flags;
1354 spin_lock_init(&stats->ls_lock);
1356 /* alloc num of counter headers */
1357 LIBCFS_ALLOC(stats->ls_cnt_header,
1358 stats->ls_num * sizeof(struct lprocfs_counter_header));
1359 if (stats->ls_cnt_header == NULL)
1362 if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1363 /* contains only one set counters */
1364 percpusize = lprocfs_stats_counter_size(stats);
1365 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1366 if (stats->ls_percpu[0] == NULL)
1368 stats->ls_biggest_alloc_num = 1;
1369 } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1370 /* alloc all percpu data, currently only obd_memory use this */
1371 for (i = 0; i < num_entry; ++i)
1372 if (lprocfs_stats_alloc_one(stats, i) < 0)
1379 lprocfs_free_stats(&stats);
1382 EXPORT_SYMBOL(lprocfs_alloc_stats);
1384 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1386 struct lprocfs_stats *stats = *statsh;
1387 unsigned int num_entry;
1388 unsigned int percpusize;
1391 if (stats == NULL || stats->ls_num == 0)
1395 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1398 num_entry = num_possible_cpus();
1400 percpusize = lprocfs_stats_counter_size(stats);
1401 for (i = 0; i < num_entry; i++)
1402 if (stats->ls_percpu[i] != NULL)
1403 LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1404 if (stats->ls_cnt_header != NULL)
1405 LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
1406 sizeof(struct lprocfs_counter_header));
1407 LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1409 EXPORT_SYMBOL(lprocfs_free_stats);
1411 u64 lprocfs_stats_collector(struct lprocfs_stats *stats, int idx,
1412 enum lprocfs_fields_flags field)
1414 unsigned long flags = 0;
1415 unsigned int num_cpu;
1421 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1422 for (i = 0; i < num_cpu; i++) {
1423 struct lprocfs_counter *cntr;
1425 if (!stats->ls_percpu[i])
1428 cntr = lprocfs_stats_counter_get(stats, i, idx);
1429 ret += lprocfs_read_helper(cntr, &stats->ls_cnt_header[idx],
1430 stats->ls_flags, field);
1432 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1435 EXPORT_SYMBOL(lprocfs_stats_collector);
1437 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1439 struct lprocfs_counter *percpu_cntr;
1442 unsigned int num_entry;
1443 unsigned long flags = 0;
1445 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1447 for (i = 0; i < num_entry; i++) {
1448 if (stats->ls_percpu[i] == NULL)
1450 for (j = 0; j < stats->ls_num; j++) {
1451 percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1452 percpu_cntr->lc_count = 0;
1453 percpu_cntr->lc_min = LC_MIN_INIT;
1454 percpu_cntr->lc_max = 0;
1455 percpu_cntr->lc_sumsquare = 0;
1456 percpu_cntr->lc_sum = 0;
1457 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1458 percpu_cntr->lc_sum_irq = 0;
1462 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1464 EXPORT_SYMBOL(lprocfs_clear_stats);
1466 static ssize_t lprocfs_stats_seq_write(struct file *file,
1467 const char __user *buf,
1468 size_t len, loff_t *off)
1470 struct seq_file *seq = file->private_data;
1471 struct lprocfs_stats *stats = seq->private;
1473 lprocfs_clear_stats(stats);
1478 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1480 struct lprocfs_stats *stats = p->private;
1482 return (*pos < stats->ls_num) ? pos : NULL;
1485 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1489 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1493 return lprocfs_stats_seq_start(p, pos);
1496 /* seq file export of one lprocfs counter */
1497 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1499 struct lprocfs_stats *stats = p->private;
1500 struct lprocfs_counter_header *hdr;
1501 struct lprocfs_counter ctr;
1502 int idx = *(loff_t *)v;
1505 struct timespec64 now;
1507 ktime_get_real_ts64(&now);
1508 seq_printf(p, "%-25s %llu.%09lu secs.nsecs\n",
1509 "snapshot_time", (s64)now.tv_sec, now.tv_nsec);
1512 hdr = &stats->ls_cnt_header[idx];
1513 lprocfs_stats_collect(stats, idx, &ctr);
1515 if (ctr.lc_count == 0)
1518 seq_printf(p, "%-25s %lld samples [%s]", hdr->lc_name,
1519 ctr.lc_count, hdr->lc_units);
1521 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1522 seq_printf(p, " %lld %lld %lld",
1523 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1524 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1525 seq_printf(p, " %llu", ctr.lc_sumsquare);
1531 static const struct seq_operations lprocfs_stats_seq_sops = {
1532 .start = lprocfs_stats_seq_start,
1533 .stop = lprocfs_stats_seq_stop,
1534 .next = lprocfs_stats_seq_next,
1535 .show = lprocfs_stats_seq_show,
1538 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1540 struct seq_file *seq;
1543 rc = LPROCFS_ENTRY_CHECK(inode);
1547 rc = seq_open(file, &lprocfs_stats_seq_sops);
1550 seq = file->private_data;
1551 seq->private = inode->i_private ? inode->i_private : PDE_DATA(inode);
1555 static const struct file_operations lprocfs_stats_seq_fops = {
1556 .owner = THIS_MODULE,
1557 .open = lprocfs_stats_seq_open,
1559 .write = lprocfs_stats_seq_write,
1560 .llseek = seq_lseek,
1561 .release = lprocfs_seq_release,
1564 int ldebugfs_register_stats(struct dentry *parent, const char *name,
1565 struct lprocfs_stats *stats)
1567 struct dentry *entry;
1569 LASSERT(!IS_ERR_OR_NULL(parent));
1571 entry = debugfs_create_file(name, 0644, parent, stats,
1572 &lprocfs_stats_seq_fops);
1573 if (IS_ERR_OR_NULL(entry))
1574 return entry ? PTR_ERR(entry) : -ENOMEM;
1578 EXPORT_SYMBOL_GPL(ldebugfs_register_stats);
1580 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1581 struct lprocfs_stats *stats)
1583 struct proc_dir_entry *entry;
1584 LASSERT(root != NULL);
1586 entry = proc_create_data(name, 0644, root,
1587 &lprocfs_stats_seq_fops, stats);
1592 EXPORT_SYMBOL(lprocfs_register_stats);
1594 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1595 unsigned conf, const char *name, const char *units)
1597 struct lprocfs_counter_header *header;
1598 struct lprocfs_counter *percpu_cntr;
1599 unsigned long flags = 0;
1601 unsigned int num_cpu;
1603 LASSERT(stats != NULL);
1605 header = &stats->ls_cnt_header[index];
1606 LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1607 index, name, units);
1609 header->lc_config = conf;
1610 header->lc_name = name;
1611 header->lc_units = units;
1613 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1614 for (i = 0; i < num_cpu; ++i) {
1615 if (stats->ls_percpu[i] == NULL)
1617 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1618 percpu_cntr->lc_count = 0;
1619 percpu_cntr->lc_min = LC_MIN_INIT;
1620 percpu_cntr->lc_max = 0;
1621 percpu_cntr->lc_sumsquare = 0;
1622 percpu_cntr->lc_sum = 0;
1623 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1624 percpu_cntr->lc_sum_irq = 0;
1626 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1628 EXPORT_SYMBOL(lprocfs_counter_init);
1630 static const char * const mps_stats[] = {
1631 [LPROC_MD_CLOSE] = "close",
1632 [LPROC_MD_CREATE] = "create",
1633 [LPROC_MD_ENQUEUE] = "enqueue",
1634 [LPROC_MD_GETATTR] = "getattr",
1635 [LPROC_MD_INTENT_LOCK] = "intent_lock",
1636 [LPROC_MD_LINK] = "link",
1637 [LPROC_MD_RENAME] = "rename",
1638 [LPROC_MD_SETATTR] = "setattr",
1639 [LPROC_MD_FSYNC] = "fsync",
1640 [LPROC_MD_READ_PAGE] = "read_page",
1641 [LPROC_MD_UNLINK] = "unlink",
1642 [LPROC_MD_SETXATTR] = "setxattr",
1643 [LPROC_MD_GETXATTR] = "getxattr",
1644 [LPROC_MD_INTENT_GETATTR_ASYNC] = "intent_getattr_async",
1645 [LPROC_MD_REVALIDATE_LOCK] = "revalidate_lock",
1648 int lprocfs_alloc_md_stats(struct obd_device *obd,
1649 unsigned int num_private_stats)
1651 struct lprocfs_stats *stats;
1652 unsigned int num_stats;
1655 /* TODO Ensure that this function is only used where
1656 * appropriate by adding an assertion to the effect that
1657 * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1658 * because mdt_procfs_init() uses this function to allocate
1659 * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1660 * mdt layer does not use the md_ops interface. This is
1661 * confusing and a waste of memory. See LU-2484.
1663 LASSERT(obd->obd_proc_entry != NULL);
1664 LASSERT(obd->obd_md_stats == NULL);
1666 num_stats = ARRAY_SIZE(mps_stats) + num_private_stats;
1667 stats = lprocfs_alloc_stats(num_stats, 0);
1671 for (i = 0; i < ARRAY_SIZE(mps_stats); i++) {
1672 lprocfs_counter_init(stats, i, 0, mps_stats[i], "reqs");
1673 if (!stats->ls_cnt_header[i].lc_name) {
1674 CERROR("Missing md_stat initializer md_op operation at offset %d. Aborting.\n",
1680 rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1682 lprocfs_free_stats(&stats);
1684 obd->obd_md_stats = stats;
1689 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1691 void lprocfs_free_md_stats(struct obd_device *obd)
1693 struct lprocfs_stats *stats = obd->obd_md_stats;
1695 if (stats != NULL) {
1696 obd->obd_md_stats = NULL;
1697 lprocfs_free_stats(&stats);
1700 EXPORT_SYMBOL(lprocfs_free_md_stats);
1702 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1704 lprocfs_counter_init(ldlm_stats,
1705 LDLM_ENQUEUE - LDLM_FIRST_OPC,
1706 0, "ldlm_enqueue", "reqs");
1707 lprocfs_counter_init(ldlm_stats,
1708 LDLM_CONVERT - LDLM_FIRST_OPC,
1709 0, "ldlm_convert", "reqs");
1710 lprocfs_counter_init(ldlm_stats,
1711 LDLM_CANCEL - LDLM_FIRST_OPC,
1712 0, "ldlm_cancel", "reqs");
1713 lprocfs_counter_init(ldlm_stats,
1714 LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1715 0, "ldlm_bl_callback", "reqs");
1716 lprocfs_counter_init(ldlm_stats,
1717 LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1718 0, "ldlm_cp_callback", "reqs");
1719 lprocfs_counter_init(ldlm_stats,
1720 LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1721 0, "ldlm_gl_callback", "reqs");
1723 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1725 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1726 struct lprocfs_counter_header *header,
1727 enum lprocfs_stats_flags flags,
1728 enum lprocfs_fields_flags field)
1732 if (lc == NULL || header == NULL)
1736 case LPROCFS_FIELDS_FLAGS_CONFIG:
1737 ret = header->lc_config;
1739 case LPROCFS_FIELDS_FLAGS_SUM:
1741 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1742 ret += lc->lc_sum_irq;
1744 case LPROCFS_FIELDS_FLAGS_MIN:
1747 case LPROCFS_FIELDS_FLAGS_MAX:
1750 case LPROCFS_FIELDS_FLAGS_AVG:
1751 ret = (lc->lc_max - lc->lc_min) / 2;
1753 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1754 ret = lc->lc_sumsquare;
1756 case LPROCFS_FIELDS_FLAGS_COUNT:
1764 EXPORT_SYMBOL(lprocfs_read_helper);
1766 int lprocfs_read_frac_helper(char *buffer, unsigned long count, long val,
1769 long decimal_val, frac_val;
1775 decimal_val = val / mult;
1776 prtn = snprintf(buffer, count, "%ld", decimal_val);
1777 frac_val = val % mult;
1779 if (prtn < (count - 4) && frac_val > 0) {
1781 int i, temp_mult = 1, frac_bits = 0;
1783 temp_frac = frac_val * 10;
1784 buffer[prtn++] = '.';
1785 while (frac_bits < 2 && (temp_frac / mult) < 1 ) {
1786 /* only reserved 2 bits fraction */
1787 buffer[prtn++] ='0';
1792 * Need to think these cases :
1793 * 1. #echo x.00 > /proc/xxx output result : x
1794 * 2. #echo x.0x > /proc/xxx output result : x.0x
1795 * 3. #echo x.x0 > /proc/xxx output result : x.x
1796 * 4. #echo x.xx > /proc/xxx output result : x.xx
1797 * Only reserved 2 bits fraction.
1799 for (i = 0; i < (5 - prtn); i++)
1802 frac_bits = min((int)count - prtn, 3 - frac_bits);
1803 prtn += snprintf(buffer + prtn, frac_bits, "%ld",
1804 frac_val * temp_mult / mult);
1807 while(buffer[prtn] < '1' || buffer[prtn] > '9') {
1809 if (buffer[prtn] == '.') {
1816 buffer[prtn++] ='\n';
1819 EXPORT_SYMBOL(lprocfs_read_frac_helper);
1821 int lprocfs_seq_read_frac_helper(struct seq_file *m, long val, int mult)
1823 long decimal_val, frac_val;
1825 decimal_val = val / mult;
1826 seq_printf(m, "%ld", decimal_val);
1827 frac_val = val % mult;
1834 /* Three cases: x0, xx, 0x */
1835 if ((frac_val % 10) != 0)
1836 seq_printf(m, ".%ld", frac_val);
1838 seq_printf(m, ".%ld", frac_val / 10);
1841 seq_printf(m, "\n");
1844 EXPORT_SYMBOL(lprocfs_seq_read_frac_helper);
1846 /* Obtains the conversion factor for the unit specified */
1847 static int get_mult(char unit, __u64 *mult)
1852 /* peta, tera, giga, mega, and kilo */
1869 /* some tests expect % to be accepted */
1883 * Ensures the numeric string is valid. The function provides the final
1884 * multiplier in the case a unit exists at the end of the string. It also
1885 * locates the start of the whole and fractional parts (if any). This
1886 * function modifies the string so kstrtoull can be used to parse both
1887 * the whole and fraction portions. This function also figures out
1888 * the base of the number.
1890 static int preprocess_numeric_str(char *buffer, __u64 *mult, __u64 def_mult,
1891 bool allow_units, char **whole, char **frac,
1894 bool hit_decimal = false;
1895 bool hit_unit = false;
1903 /* a hex string if it starts with "0x" */
1904 if (buffer[0] == '0' && tolower(buffer[1]) == 'x') {
1912 /* allow for a single new line before the null terminator */
1913 if (*buffer == '\n') {
1923 /* any chars after our unit indicates a malformed string */
1927 /* ensure we only hit one decimal */
1928 if (*buffer == '.') {
1932 /* if past start, there's a whole part */
1933 if (start != buffer)
1939 } else if (!isdigit(*buffer) &&
1940 !(*base == 16 && isxdigit(*buffer))) {
1942 /* if we allow units, attempt to get mult */
1944 rc = get_mult(*buffer, mult);
1948 /* string stops here, but keep processing */
1960 /* hit a decimal, make sure there's a fractional part */
1966 /* didn't hit a decimal, but may have a whole part */
1967 if (start != buffer && *start)
1971 /* malformed string if we didn't get anything */
1972 if (!*frac && !*whole)
1979 * Parses a numeric string which can contain a whole and fraction portion
1980 * into a __u64. Accepts a multiplier to apply to the value parsed. Also
1981 * allows the string to have a unit at the end. The function handles
1982 * wrapping of the final unsigned value.
1984 static int str_to_u64_parse(char *buffer, unsigned long count,
1985 __u64 *val, __u64 def_mult, bool allow_units)
1989 unsigned int frac_d = 1;
1990 __u64 wrap_indicator = ULLONG_MAX;
1995 unsigned int base = 10;
1997 rc = preprocess_numeric_str(buffer, &mult, def_mult, allow_units,
1998 &strwhole, &strfrac, &base);
2008 /* the multiplier limits how large the value can be */
2009 wrap_indicator = div64_u64(wrap_indicator, mult);
2012 rc = kstrtoull(strwhole, base, &whole);
2016 if (whole > wrap_indicator)
2023 if (strlen(strfrac) > 10)
2026 rc = kstrtoull(strfrac, base, &frac);
2030 /* determine power of fractional portion */
2036 /* fractional portion is too large to perform calculation */
2037 if (frac > wrap_indicator)
2041 do_div(frac, frac_d);
2044 /* check that the sum of whole and fraction fits in u64 */
2045 if (whole > (ULLONG_MAX - frac))
2048 *val = whole + frac;
2054 * This function parses numeric/hex strings into __s64. It accepts a multiplier
2055 * which will apply to the value parsed. It also can allow the string to
2056 * have a unit as the last character. The function handles overflow/underflow
2057 * of the signed integer.
2059 static int str_to_s64_internal(const char __user *buffer, unsigned long count,
2060 __s64 *val, __u64 def_mult, bool allow_units)
2064 unsigned int offset = 0;
2065 int signed sign = 1;
2066 __u64 max = LLONG_MAX;
2069 if (count > (sizeof(kernbuf) - 1))
2072 if (copy_from_user(kernbuf, buffer, count))
2075 kernbuf[count] = '\0';
2077 /* keep track of our sign */
2078 if (*kernbuf == '-') {
2081 /* equivalent to max = -LLONG_MIN, avoids overflow */
2085 rc = str_to_u64_parse(kernbuf + offset, count - offset,
2086 &tmp, def_mult, allow_units);
2090 /* check for overflow/underflow */
2094 *val = (__s64)tmp * sign;
2100 * Convert a user string into a signed 64 bit number. This function produces
2101 * an error when the value parsed from the string times multiplier underflows or
2102 * overflows. This function only accepts strings that contains digits, an
2103 * optional decimal, and a char representing a unit at the end. If a unit is
2104 * specified in the string, the multiplier provided by the caller is ignored.
2105 * This function can also accept hexadecimal strings which are prefixed with
2108 * \param[in] buffer string consisting of numbers, a decimal, and a unit
2109 * \param[in] count buffer length
2110 * \param[in] val if successful, the value represented by the string
2111 * \param[in] defunit default unit if string doesn't contain one
2113 * \retval 0 on success
2114 * \retval negative number on error
2116 int lprocfs_str_with_units_to_s64(const char __user *buffer,
2117 unsigned long count, __s64 *val, char defunit)
2122 if (defunit != '1') {
2123 rc = get_mult(defunit, &mult);
2128 return str_to_s64_internal(buffer, count, val, mult, true);
2130 EXPORT_SYMBOL(lprocfs_str_with_units_to_s64);
2132 char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
2141 if (!memcmp(s1, s2, l2))
2147 EXPORT_SYMBOL(lprocfs_strnstr);
2150 * Find the string \a name in the input \a buffer, and return a pointer to the
2151 * value immediately following \a name, reducing \a count appropriately.
2152 * If \a name is not found the original \a buffer is returned.
2154 char *lprocfs_find_named_value(const char *buffer, const char *name,
2158 size_t buflen = *count;
2160 /* there is no strnstr() in rhel5 and ubuntu kernels */
2161 val = lprocfs_strnstr(buffer, name, buflen);
2163 return (char *)buffer;
2165 val += strlen(name); /* skip prefix */
2166 while (val < buffer + buflen && isspace(*val)) /* skip separator */
2170 while (val < buffer + buflen && isalnum(*val)) {
2175 return val - *count;
2177 EXPORT_SYMBOL(lprocfs_find_named_value);
2179 int ldebugfs_seq_create(struct dentry *parent, const char *name, umode_t mode,
2180 const struct file_operations *seq_fops, void *data)
2182 struct dentry *entry;
2184 /* Disallow secretly (un)writable entries. */
2185 LASSERT((!seq_fops->write) == (!(mode & 0222)));
2187 entry = debugfs_create_file(name, mode, parent, data, seq_fops);
2188 if (IS_ERR_OR_NULL(entry))
2189 return entry ? PTR_ERR(entry) : -ENOMEM;
2193 EXPORT_SYMBOL_GPL(ldebugfs_seq_create);
2195 int lprocfs_seq_create(struct proc_dir_entry *parent,
2198 const struct file_operations *seq_fops,
2201 struct proc_dir_entry *entry;
2204 /* Disallow secretly (un)writable entries. */
2205 LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
2207 entry = proc_create_data(name, mode, parent, seq_fops, data);
2214 EXPORT_SYMBOL(lprocfs_seq_create);
2216 int lprocfs_obd_seq_create(struct obd_device *dev,
2219 const struct file_operations *seq_fops,
2222 return (lprocfs_seq_create(dev->obd_proc_entry, name,
2223 mode, seq_fops, data));
2225 EXPORT_SYMBOL(lprocfs_obd_seq_create);
2227 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
2229 if (value >= OBD_HIST_MAX)
2230 value = OBD_HIST_MAX - 1;
2232 spin_lock(&oh->oh_lock);
2233 oh->oh_buckets[value]++;
2234 spin_unlock(&oh->oh_lock);
2236 EXPORT_SYMBOL(lprocfs_oh_tally);
2238 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
2240 unsigned int val = 0;
2242 if (likely(value != 0))
2243 val = min(fls(value - 1), OBD_HIST_MAX);
2245 lprocfs_oh_tally(oh, val);
2247 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
2249 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
2251 unsigned long ret = 0;
2254 for (i = 0; i < OBD_HIST_MAX; i++)
2255 ret += oh->oh_buckets[i];
2258 EXPORT_SYMBOL(lprocfs_oh_sum);
2260 void lprocfs_oh_clear(struct obd_histogram *oh)
2262 spin_lock(&oh->oh_lock);
2263 memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
2264 spin_unlock(&oh->oh_lock);
2266 EXPORT_SYMBOL(lprocfs_oh_clear);
2268 ssize_t lustre_attr_show(struct kobject *kobj,
2269 struct attribute *attr, char *buf)
2271 struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
2273 return a->show ? a->show(kobj, attr, buf) : 0;
2275 EXPORT_SYMBOL_GPL(lustre_attr_show);
2277 ssize_t lustre_attr_store(struct kobject *kobj, struct attribute *attr,
2278 const char *buf, size_t len)
2280 struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
2282 return a->store ? a->store(kobj, attr, buf, len) : len;
2284 EXPORT_SYMBOL_GPL(lustre_attr_store);
2286 const struct sysfs_ops lustre_sysfs_ops = {
2287 .show = lustre_attr_show,
2288 .store = lustre_attr_store,
2290 EXPORT_SYMBOL_GPL(lustre_sysfs_ops);
2292 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
2294 struct obd_device *dev = data;
2295 struct client_obd *cli = &dev->u.cli;
2297 spin_lock(&cli->cl_loi_list_lock);
2298 seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
2299 spin_unlock(&cli->cl_loi_list_lock);
2302 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
2304 ssize_t lprocfs_obd_max_pages_per_rpc_seq_write(struct file *file,
2305 const char __user *buffer,
2306 size_t count, loff_t *off)
2308 struct obd_device *dev =
2309 ((struct seq_file *)file->private_data)->private;
2310 struct client_obd *cli = &dev->u.cli;
2311 struct obd_connect_data *ocd = &cli->cl_import->imp_connect_data;
2315 rc = lprocfs_str_with_units_to_s64(buffer, count, &val, '1');
2321 /* if the max_pages is specified in bytes, convert to pages */
2322 if (val >= ONE_MB_BRW_SIZE)
2325 LPROCFS_CLIMP_CHECK(dev);
2327 chunk_mask = ~((1 << (cli->cl_chunkbits - PAGE_SHIFT)) - 1);
2328 /* max_pages_per_rpc must be chunk aligned */
2329 val = (val + ~chunk_mask) & chunk_mask;
2330 if (val == 0 || (ocd->ocd_brw_size != 0 &&
2331 val > ocd->ocd_brw_size >> PAGE_SHIFT)) {
2332 LPROCFS_CLIMP_EXIT(dev);
2335 spin_lock(&cli->cl_loi_list_lock);
2336 cli->cl_max_pages_per_rpc = val;
2337 client_adjust_max_dirty(cli);
2338 spin_unlock(&cli->cl_loi_list_lock);
2340 LPROCFS_CLIMP_EXIT(dev);
2343 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_write);
2345 ssize_t short_io_bytes_show(struct kobject *kobj, struct attribute *attr,
2348 struct obd_device *dev = container_of(kobj, struct obd_device,
2350 struct client_obd *cli = &dev->u.cli;
2353 spin_lock(&cli->cl_loi_list_lock);
2354 rc = sprintf(buf, "%d\n", cli->cl_max_short_io_bytes);
2355 spin_unlock(&cli->cl_loi_list_lock);
2358 EXPORT_SYMBOL(short_io_bytes_show);
2360 /* Used to catch people who think they're specifying pages. */
2361 #define MIN_SHORT_IO_BYTES 64
2363 ssize_t short_io_bytes_store(struct kobject *kobj, struct attribute *attr,
2364 const char *buffer, size_t count)
2366 struct obd_device *dev = container_of(kobj, struct obd_device,
2368 struct client_obd *cli = &dev->u.cli;
2372 LPROCFS_CLIMP_CHECK(dev);
2374 rc = kstrtouint(buffer, 0, &val);
2378 if (val && (val < MIN_SHORT_IO_BYTES || val > OBD_MAX_SHORT_IO_BYTES))
2379 GOTO(out, rc = -ERANGE);
2383 spin_lock(&cli->cl_loi_list_lock);
2384 if (val > (cli->cl_max_pages_per_rpc << PAGE_SHIFT))
2387 cli->cl_max_short_io_bytes = val;
2388 spin_unlock(&cli->cl_loi_list_lock);
2391 LPROCFS_CLIMP_EXIT(dev);
2394 EXPORT_SYMBOL(short_io_bytes_store);
2396 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2397 struct root_squash_info *squash, char *name)
2400 char kernbuf[64], *tmp, *errmsg;
2401 unsigned long uid, gid;
2404 if (count >= sizeof(kernbuf)) {
2405 errmsg = "string too long";
2406 GOTO(failed_noprint, rc = -EINVAL);
2408 if (copy_from_user(kernbuf, buffer, count)) {
2409 errmsg = "bad address";
2410 GOTO(failed_noprint, rc = -EFAULT);
2412 kernbuf[count] = '\0';
2414 /* look for uid gid separator */
2415 tmp = strchr(kernbuf, ':');
2417 errmsg = "needs uid:gid format";
2418 GOTO(failed, rc = -EINVAL);
2424 if (kstrtoul(kernbuf, 0, &uid) != 0) {
2426 GOTO(failed, rc = -EINVAL);
2430 if (kstrtoul(tmp, 0, &gid) != 0) {
2432 GOTO(failed, rc = -EINVAL);
2435 squash->rsi_uid = uid;
2436 squash->rsi_gid = gid;
2438 LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2439 name, squash->rsi_uid, squash->rsi_gid);
2447 CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2448 name, kernbuf, errmsg, rc);
2451 CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2455 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2458 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2459 struct root_squash_info *squash, char *name)
2462 char *kernbuf = NULL;
2464 struct list_head tmp;
2469 errmsg = "string too long";
2470 GOTO(failed, rc = -EINVAL);
2473 OBD_ALLOC(kernbuf, count + 1);
2474 if (kernbuf == NULL) {
2475 errmsg = "no memory";
2476 GOTO(failed, rc = -ENOMEM);
2478 if (copy_from_user(kernbuf, buffer, count)) {
2479 errmsg = "bad address";
2480 GOTO(failed, rc = -EFAULT);
2482 kernbuf[count] = '\0';
2484 if (count > 0 && kernbuf[count - 1] == '\n')
2487 if ((len == 4 && strncmp(kernbuf, "NONE", len) == 0) ||
2488 (len == 5 && strncmp(kernbuf, "clear", len) == 0)) {
2489 /* empty string is special case */
2490 down_write(&squash->rsi_sem);
2491 if (!list_empty(&squash->rsi_nosquash_nids))
2492 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2493 up_write(&squash->rsi_sem);
2494 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2495 OBD_FREE(kernbuf, count + 1);
2499 INIT_LIST_HEAD(&tmp);
2500 if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2501 errmsg = "can't parse";
2502 GOTO(failed, rc = -EINVAL);
2504 LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2506 OBD_FREE(kernbuf, count + 1);
2509 down_write(&squash->rsi_sem);
2510 if (!list_empty(&squash->rsi_nosquash_nids))
2511 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2512 list_splice(&tmp, &squash->rsi_nosquash_nids);
2513 up_write(&squash->rsi_sem);
2519 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2520 name, kernbuf, errmsg, rc);
2521 OBD_FREE(kernbuf, count + 1);
2523 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2528 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2530 #endif /* CONFIG_PROC_FS*/