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 lprocfs_remove(struct proc_dir_entry **rooth)
215 EXPORT_SYMBOL(lprocfs_remove);
217 void lprocfs_remove_proc_entry(const char *name, struct proc_dir_entry *parent)
219 LASSERT(parent != NULL);
220 remove_proc_entry(name, parent);
222 EXPORT_SYMBOL(lprocfs_remove_proc_entry);
224 struct proc_dir_entry *
225 lprocfs_register(const char *name, struct proc_dir_entry *parent,
226 struct lprocfs_vars *list, void *data)
228 struct proc_dir_entry *newchild;
230 newchild = proc_mkdir(name, parent);
232 return ERR_PTR(-ENOMEM);
235 int rc = lprocfs_add_vars(newchild, list, data);
237 lprocfs_remove(&newchild);
243 EXPORT_SYMBOL(lprocfs_register);
245 /* Generic callbacks */
246 int lprocfs_uuid_seq_show(struct seq_file *m, void *data)
248 struct obd_device *obd = data;
250 LASSERT(obd != NULL);
251 seq_printf(m, "%s\n", obd->obd_uuid.uuid);
254 EXPORT_SYMBOL(lprocfs_uuid_seq_show);
256 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
259 struct obd_device *obd = container_of(kobj, struct obd_device,
262 return sprintf(buf, "%s\n", obd->obd_uuid.uuid);
264 LUSTRE_RO_ATTR(uuid);
266 static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
269 struct obd_device *obd = container_of(kobj, struct obd_device,
271 struct obd_statfs osfs;
274 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
275 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
278 return sprintf(buf, "%u\n", osfs.os_bsize);
282 LUSTRE_RO_ATTR(blocksize);
284 static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
287 struct obd_device *obd = container_of(kobj, struct obd_device,
289 struct obd_statfs osfs;
292 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
293 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
296 u32 blk_size = osfs.os_bsize >> 10;
297 u64 result = osfs.os_blocks;
299 result *= rounddown_pow_of_two(blk_size ?: 1);
300 return sprintf(buf, "%llu\n", result);
305 LUSTRE_RO_ATTR(kbytestotal);
307 static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
310 struct obd_device *obd = container_of(kobj, struct obd_device,
312 struct obd_statfs osfs;
315 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
316 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
319 u32 blk_size = osfs.os_bsize >> 10;
320 u64 result = osfs.os_bfree;
322 while (blk_size >>= 1)
325 return sprintf(buf, "%llu\n", result);
330 LUSTRE_RO_ATTR(kbytesfree);
332 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
335 struct obd_device *obd = container_of(kobj, struct obd_device,
337 struct obd_statfs osfs;
340 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
341 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
344 u32 blk_size = osfs.os_bsize >> 10;
345 u64 result = osfs.os_bavail;
347 while (blk_size >>= 1)
350 return sprintf(buf, "%llu\n", result);
355 LUSTRE_RO_ATTR(kbytesavail);
357 static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
360 struct obd_device *obd = container_of(kobj, struct obd_device,
362 struct obd_statfs osfs;
365 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
366 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
369 return sprintf(buf, "%llu\n", osfs.os_files);
373 LUSTRE_RO_ATTR(filestotal);
375 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
378 struct obd_device *obd = container_of(kobj, struct obd_device,
380 struct obd_statfs osfs;
383 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
384 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
387 return sprintf(buf, "%llu\n", osfs.os_ffree);
391 LUSTRE_RO_ATTR(filesfree);
393 ssize_t conn_uuid_show(struct kobject *kobj, struct attribute *attr, char *buf)
395 struct obd_device *obd = container_of(kobj, struct obd_device,
397 struct obd_import *imp;
398 struct ptlrpc_connection *conn;
401 with_imp_locked(obd, imp, count) {
402 conn = imp->imp_connection;
404 count = sprintf(buf, "%s\n", conn->c_remote_uuid.uuid);
406 count = sprintf(buf, "%s\n", "<none>");
411 EXPORT_SYMBOL(conn_uuid_show);
413 int lprocfs_server_uuid_seq_show(struct seq_file *m, void *data)
415 struct obd_device *obd = data;
416 struct obd_import *imp;
417 char *imp_state_name = NULL;
420 LASSERT(obd != NULL);
421 with_imp_locked(obd, imp, rc) {
422 imp_state_name = ptlrpc_import_state_name(imp->imp_state);
423 seq_printf(m, "%s\t%s%s\n", obd2cli_tgt(obd), imp_state_name,
424 imp->imp_deactive ? "\tDEACTIVATED" : "");
429 EXPORT_SYMBOL(lprocfs_server_uuid_seq_show);
431 /** add up per-cpu counters */
434 * Lock statistics structure for access, possibly only on this CPU.
436 * The statistics struct may be allocated with per-CPU structures for
437 * efficient concurrent update (usually only on server-wide stats), or
438 * as a single global struct (e.g. for per-client or per-job statistics),
439 * so the required locking depends on the type of structure allocated.
441 * For per-CPU statistics, pin the thread to the current cpuid so that
442 * will only access the statistics for that CPU. If the stats structure
443 * for the current CPU has not been allocated (or previously freed),
444 * allocate it now. The per-CPU statistics do not need locking since
445 * the thread is pinned to the CPU during update.
447 * For global statistics, lock the stats structure to prevent concurrent update.
449 * \param[in] stats statistics structure to lock
450 * \param[in] opc type of operation:
451 * LPROCFS_GET_SMP_ID: "lock" and return current CPU index
452 * for incrementing statistics for that CPU
453 * LPROCFS_GET_NUM_CPU: "lock" and return number of used
454 * CPU indices to iterate over all indices
455 * \param[out] flags CPU interrupt saved state for IRQ-safe locking
457 * \retval cpuid of current thread or number of allocated structs
458 * \retval negative on error (only for opc LPROCFS_GET_SMP_ID + per-CPU stats)
460 int lprocfs_stats_lock(struct lprocfs_stats *stats,
461 enum lprocfs_stats_lock_ops opc,
462 unsigned long *flags)
464 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
465 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
466 spin_lock_irqsave(&stats->ls_lock, *flags);
468 spin_lock(&stats->ls_lock);
469 return opc == LPROCFS_GET_NUM_CPU ? 1 : 0;
473 case LPROCFS_GET_SMP_ID: {
474 unsigned int cpuid = get_cpu();
476 if (unlikely(!stats->ls_percpu[cpuid])) {
477 int rc = lprocfs_stats_alloc_one(stats, cpuid);
486 case LPROCFS_GET_NUM_CPU:
487 return stats->ls_biggest_alloc_num;
494 * Unlock statistics structure after access.
496 * Unlock the lock acquired via lprocfs_stats_lock() for global statistics,
497 * or unpin this thread from the current cpuid for per-CPU statistics.
499 * This function must be called using the same arguments as used when calling
500 * lprocfs_stats_lock() so that the correct operation can be performed.
502 * \param[in] stats statistics structure to unlock
503 * \param[in] opc type of operation (current cpuid or number of structs)
504 * \param[in] flags CPU interrupt saved state for IRQ-safe locking
506 void lprocfs_stats_unlock(struct lprocfs_stats *stats,
507 enum lprocfs_stats_lock_ops opc,
508 unsigned long *flags)
510 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
511 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
512 spin_unlock_irqrestore(&stats->ls_lock, *flags);
514 spin_unlock(&stats->ls_lock);
515 } else if (opc == LPROCFS_GET_SMP_ID) {
520 /** add up per-cpu counters */
521 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
522 struct lprocfs_counter *cnt)
524 unsigned int num_entry;
525 struct lprocfs_counter *percpu_cntr;
527 unsigned long flags = 0;
529 memset(cnt, 0, sizeof(*cnt));
532 /* set count to 1 to avoid divide-by-zero errs in callers */
537 cnt->lc_min = LC_MIN_INIT;
539 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
541 for (i = 0; i < num_entry; i++) {
542 if (!stats->ls_percpu[i])
544 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
546 cnt->lc_count += percpu_cntr->lc_count;
547 cnt->lc_sum += percpu_cntr->lc_sum;
548 if (percpu_cntr->lc_min < cnt->lc_min)
549 cnt->lc_min = percpu_cntr->lc_min;
550 if (percpu_cntr->lc_max > cnt->lc_max)
551 cnt->lc_max = percpu_cntr->lc_max;
552 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
555 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
558 static void obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
562 if (imp->imp_obd->obd_no_recov) {
563 seq_printf(m, "no_recov");
567 flag2str(imp, invalid);
568 flag2str(imp, deactive);
569 flag2str(imp, replayable);
570 flag2str(imp, delayed_recovery);
571 flag2str(imp, vbr_failed);
572 flag2str(imp, pingable);
573 flag2str(imp, resend_replay);
574 flag2str(imp, no_pinger_recover);
575 flag2str(imp, connect_tried);
578 static const char *obd_connect_names[] = {
597 "remote_client_by_force",
606 "mds_mds_connection",
609 "alt_checksum_algorithm",
645 "file_secctx", /* 0x01 */
646 "lockaheadv2", /* 0x02 */
647 "dir_migrate", /* 0x04 */
648 "sum_statfs", /* 0x08 */
649 "overstriping", /* 0x10 */
652 "lock_convert", /* 0x80 */
653 "archive_id_array", /* 0x100 */
654 "increasing_xid", /* 0x200 */
655 "selinux_policy", /* 0x400 */
658 "crush", /* 0x2000 */
659 "async_discard", /* 0x4000 */
660 "client_encryption", /* 0x8000 */
664 void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, __u64 flags2,
671 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
673 seq_printf(m, "%s%s",
674 first ? "" : sep, obd_connect_names[i]);
679 if (flags & ~(mask - 1)) {
680 seq_printf(m, "%sunknown_%#llx",
681 first ? "" : sep, flags & ~(mask - 1));
685 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
688 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
690 seq_printf(m, "%s%s",
691 first ? "" : sep, obd_connect_names[i]);
696 if (flags2 & ~(mask - 1)) {
697 seq_printf(m, "%sunknown2_%#llx",
698 first ? "" : sep, flags2 & ~(mask - 1));
702 EXPORT_SYMBOL(obd_connect_seq_flags2str);
704 int obd_connect_flags2str(char *page, int count, __u64 flags, __u64 flags2,
710 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
712 ret += snprintf(page + ret, count - ret, "%s%s",
713 ret ? sep : "", obd_connect_names[i]);
716 if (flags & ~(mask - 1))
717 ret += snprintf(page + ret, count - ret,
719 ret ? sep : "", flags & ~(mask - 1));
721 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
724 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
726 ret += snprintf(page + ret, count - ret, "%s%s",
727 ret ? sep : "", obd_connect_names[i]);
730 if (flags2 & ~(mask - 1))
731 ret += snprintf(page + ret, count - ret,
733 ret ? sep : "", flags2 & ~(mask - 1));
737 EXPORT_SYMBOL(obd_connect_flags2str);
740 obd_connect_data_seqprint(struct seq_file *m, struct obd_connect_data *ocd)
744 LASSERT(ocd != NULL);
745 flags = ocd->ocd_connect_flags;
747 seq_printf(m, " connect_data:\n"
750 ocd->ocd_connect_flags,
752 if (flags & OBD_CONNECT_VERSION)
753 seq_printf(m, " target_version: %u.%u.%u.%u\n",
754 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
755 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
756 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
757 OBD_OCD_VERSION_FIX(ocd->ocd_version));
758 if (flags & OBD_CONNECT_MDS)
759 seq_printf(m, " mdt_index: %d\n", ocd->ocd_group);
760 if (flags & OBD_CONNECT_GRANT)
761 seq_printf(m, " initial_grant: %d\n", ocd->ocd_grant);
762 if (flags & OBD_CONNECT_INDEX)
763 seq_printf(m, " target_index: %u\n", ocd->ocd_index);
764 if (flags & OBD_CONNECT_BRW_SIZE)
765 seq_printf(m, " max_brw_size: %d\n", ocd->ocd_brw_size);
766 if (flags & OBD_CONNECT_IBITS)
767 seq_printf(m, " ibits_known: %#llx\n",
768 ocd->ocd_ibits_known);
769 if (flags & OBD_CONNECT_GRANT_PARAM)
770 seq_printf(m, " grant_block_size: %d\n"
771 " grant_inode_size: %d\n"
772 " grant_max_extent_size: %d\n"
773 " grant_extent_tax: %d\n",
774 1 << ocd->ocd_grant_blkbits,
775 1 << ocd->ocd_grant_inobits,
776 ocd->ocd_grant_max_blks << ocd->ocd_grant_blkbits,
777 ocd->ocd_grant_tax_kb << 10);
778 if (flags & OBD_CONNECT_TRANSNO)
779 seq_printf(m, " first_transno: %#llx\n",
781 if (flags & OBD_CONNECT_CKSUM)
782 seq_printf(m, " cksum_types: %#x\n",
783 ocd->ocd_cksum_types);
784 if (flags & OBD_CONNECT_MAX_EASIZE)
785 seq_printf(m, " max_easize: %d\n", ocd->ocd_max_easize);
786 if (flags & OBD_CONNECT_MAXBYTES)
787 seq_printf(m, " max_object_bytes: %llu\n",
789 if (flags & OBD_CONNECT_MULTIMODRPCS)
790 seq_printf(m, " max_mod_rpcs: %hu\n",
791 ocd->ocd_maxmodrpcs);
794 static void lprocfs_import_seq_show_locked(struct seq_file *m,
795 struct obd_device *obd,
796 struct obd_import *imp)
798 char nidstr[LNET_NIDSTR_SIZE];
799 struct lprocfs_counter ret;
800 struct lprocfs_counter_header *header;
801 struct obd_import_conn *conn;
802 struct obd_connect_data *ocd;
807 ocd = &imp->imp_connect_data;
809 seq_printf(m, "import:\n"
813 " connect_flags: [ ",
816 ptlrpc_import_state_name(imp->imp_state));
817 obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
818 imp->imp_connect_data.ocd_connect_flags2,
820 seq_printf(m, " ]\n");
821 obd_connect_data_seqprint(m, ocd);
822 seq_printf(m, " import_flags: [ ");
823 obd_import_flags2str(imp, m);
827 " failover_nids: [ ");
828 spin_lock(&imp->imp_lock);
830 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
831 libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
832 nidstr, sizeof(nidstr));
833 seq_printf(m, "%s%s", j ? ", " : "", nidstr);
836 if (imp->imp_connection)
837 libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
838 nidstr, sizeof(nidstr));
840 strncpy(nidstr, "<none>", sizeof(nidstr));
842 " current_connection: %s\n"
843 " connection_attempts: %u\n"
845 " in-progress_invalidations: %u\n"
850 atomic_read(&imp->imp_inval_count),
851 ktime_get_real_seconds() - imp->imp_last_reply_time);
852 spin_unlock(&imp->imp_lock);
854 if (!obd->obd_svc_stats)
857 header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
858 lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
859 if (ret.lc_count != 0)
860 ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
863 seq_printf(m, " rpcs:\n"
865 " unregistering: %u\n"
867 " avg_waittime: %llu %s\n",
868 atomic_read(&imp->imp_inflight),
869 atomic_read(&imp->imp_unregistering),
870 atomic_read(&imp->imp_timeouts),
871 ret.lc_sum, header->lc_units);
874 for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
875 if (imp->imp_at.iat_portal[j] == 0)
877 k = max_t(unsigned int, k,
878 at_get(&imp->imp_at.iat_service_estimate[j]));
880 seq_printf(m, " service_estimates:\n"
881 " services: %u sec\n"
882 " network: %u sec\n",
884 at_get(&imp->imp_at.iat_net_latency));
886 seq_printf(m, " transactions:\n"
887 " last_replay: %llu\n"
888 " peer_committed: %llu\n"
889 " last_checked: %llu\n",
890 imp->imp_last_replay_transno,
891 imp->imp_peer_committed_transno,
892 imp->imp_last_transno_checked);
895 for (rw = 0; rw <= 1; rw++) {
896 lprocfs_stats_collect(obd->obd_svc_stats,
897 PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
899 if (ret.lc_sum > 0 && ret.lc_count > 0) {
900 ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
901 seq_printf(m, " %s_data_averages:\n"
902 " bytes_per_rpc: %llu\n",
903 rw ? "write" : "read",
907 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
908 header = &obd->obd_svc_stats->ls_cnt_header[j];
909 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
910 if (ret.lc_sum > 0 && ret.lc_count != 0) {
911 ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
912 seq_printf(m, " %s_per_rpc: %llu\n",
913 header->lc_units, ret.lc_sum);
916 seq_printf(m, " MB_per_sec: %u.%.02u\n",
917 k / j, (100 * k / j) % 100);
922 int lprocfs_import_seq_show(struct seq_file *m, void *data)
924 struct obd_device *obd = (struct obd_device *)data;
925 struct obd_import *imp;
928 LASSERT(obd != NULL);
929 with_imp_locked(obd, imp, rv)
930 lprocfs_import_seq_show_locked(m, obd, imp);
933 EXPORT_SYMBOL(lprocfs_import_seq_show);
935 int lprocfs_state_seq_show(struct seq_file *m, void *data)
937 struct obd_device *obd = (struct obd_device *)data;
938 struct obd_import *imp;
942 LASSERT(obd != NULL);
943 with_imp_locked(obd, imp, rc) {
944 seq_printf(m, "current_state: %s\n",
945 ptlrpc_import_state_name(imp->imp_state));
946 seq_printf(m, "state_history:\n");
947 k = imp->imp_state_hist_idx;
948 for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
949 struct import_state_hist *ish =
950 &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
951 if (ish->ish_state == 0)
953 seq_printf(m, " - [ %lld, %s ]\n", (s64)ish->ish_time,
954 ptlrpc_import_state_name(ish->ish_state));
960 EXPORT_SYMBOL(lprocfs_state_seq_show);
962 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
965 for (i = 0; i < AT_BINS; i++)
966 seq_printf(m, "%3u ", at->at_hist[i]);
970 EXPORT_SYMBOL(lprocfs_at_hist_helper);
972 /* See also ptlrpc_lprocfs_timeouts_show_seq */
973 static void lprocfs_timeouts_seq_show_locked(struct seq_file *m,
974 struct obd_device *obd,
975 struct obd_import *imp)
977 unsigned int cur, worst;
978 time64_t now, worstt;
981 LASSERT(obd != NULL);
983 now = ktime_get_real_seconds();
985 /* Some network health info for kicks */
986 seq_printf(m, "%-10s : %lld, %llds ago\n",
987 "last reply", (s64)imp->imp_last_reply_time,
988 (s64)(now - imp->imp_last_reply_time));
990 cur = at_get(&imp->imp_at.iat_net_latency);
991 worst = imp->imp_at.iat_net_latency.at_worst_ever;
992 worstt = imp->imp_at.iat_net_latency.at_worst_time;
993 seq_printf(m, "%-10s : cur %3u worst %3u (at %lld, %llds ago) ",
994 "network", cur, worst, (s64)worstt, (s64)(now - worstt));
995 lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
997 for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
998 if (imp->imp_at.iat_portal[i] == 0)
1000 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
1001 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
1002 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
1003 seq_printf(m, "portal %-2d : cur %3u worst %3u (at %lld, %llds ago) ",
1004 imp->imp_at.iat_portal[i], cur, worst, (s64)worstt,
1005 (s64)(now - worstt));
1006 lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
1010 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
1012 struct obd_device *obd = (struct obd_device *)data;
1013 struct obd_import *imp;
1016 with_imp_locked(obd, imp, rc)
1017 lprocfs_timeouts_seq_show_locked(m, obd, imp);
1020 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
1022 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
1024 struct obd_device *obd = data;
1027 struct obd_import *imp;
1030 with_imp_locked(obd, imp, rc) {
1031 flags = imp->imp_connect_data.ocd_connect_flags;
1032 flags2 = imp->imp_connect_data.ocd_connect_flags2;
1033 seq_printf(m, "flags=%#llx\n", flags);
1034 seq_printf(m, "flags2=%#llx\n", flags2);
1035 obd_connect_seq_flags2str(m, flags, flags2, "\n");
1036 seq_printf(m, "\n");
1041 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
1043 static const struct attribute *obd_def_uuid_attrs[] = {
1044 &lustre_attr_uuid.attr,
1048 static const struct attribute *obd_def_attrs[] = {
1049 &lustre_attr_blocksize.attr,
1050 &lustre_attr_kbytestotal.attr,
1051 &lustre_attr_kbytesfree.attr,
1052 &lustre_attr_kbytesavail.attr,
1053 &lustre_attr_filestotal.attr,
1054 &lustre_attr_filesfree.attr,
1055 &lustre_attr_uuid.attr,
1059 static void obd_sysfs_release(struct kobject *kobj)
1061 struct obd_device *obd = container_of(kobj, struct obd_device,
1064 complete(&obd->obd_kobj_unregister);
1067 int lprocfs_obd_setup(struct obd_device *obd, bool uuid_only)
1069 struct lprocfs_vars *debugfs_vars = NULL;
1072 if (!obd || obd->obd_magic != OBD_DEVICE_MAGIC)
1075 rc = kobject_set_name(&obd->obd_kset.kobj, "%s", obd->obd_name);
1079 obd->obd_ktype.sysfs_ops = &lustre_sysfs_ops;
1080 obd->obd_ktype.release = obd_sysfs_release;
1082 obd->obd_kset.kobj.parent = &obd->obd_type->typ_kobj;
1083 obd->obd_kset.kobj.ktype = &obd->obd_ktype;
1084 init_completion(&obd->obd_kobj_unregister);
1085 rc = kset_register(&obd->obd_kset);
1090 obd->obd_attrs = obd_def_uuid_attrs;
1092 obd->obd_attrs = obd_def_attrs;
1094 rc = sysfs_create_files(&obd->obd_kset.kobj, obd->obd_attrs);
1096 kset_unregister(&obd->obd_kset);
1100 if (!obd->obd_type->typ_procroot)
1101 debugfs_vars = obd->obd_vars;
1102 obd->obd_debugfs_entry = debugfs_create_dir(
1103 obd->obd_name, obd->obd_type->typ_debugfs_entry);
1104 ldebugfs_add_vars(obd->obd_debugfs_entry, debugfs_vars, obd);
1106 if (obd->obd_proc_entry || !obd->obd_type->typ_procroot)
1107 GOTO(already_registered, rc);
1109 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1110 obd->obd_type->typ_procroot,
1111 obd->obd_vars, obd);
1112 if (IS_ERR(obd->obd_proc_entry)) {
1113 rc = PTR_ERR(obd->obd_proc_entry);
1114 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1115 obd->obd_proc_entry = NULL;
1117 debugfs_remove_recursive(obd->obd_debugfs_entry);
1118 obd->obd_debugfs_entry = NULL;
1120 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1121 obd->obd_attrs = NULL;
1122 kset_unregister(&obd->obd_kset);
1128 EXPORT_SYMBOL(lprocfs_obd_setup);
1130 int lprocfs_obd_cleanup(struct obd_device *obd)
1135 if (obd->obd_proc_exports_entry) {
1136 /* Should be no exports left */
1137 lprocfs_remove(&obd->obd_proc_exports_entry);
1138 obd->obd_proc_exports_entry = NULL;
1141 if (obd->obd_proc_entry) {
1142 lprocfs_remove(&obd->obd_proc_entry);
1143 obd->obd_proc_entry = NULL;
1146 debugfs_remove_recursive(obd->obd_debugfs_entry);
1147 obd->obd_debugfs_entry = NULL;
1149 /* obd device never allocated a kset */
1150 if (!obd->obd_kset.kobj.state_initialized)
1153 if (obd->obd_attrs) {
1154 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1155 obd->obd_attrs = NULL;
1158 kset_unregister(&obd->obd_kset);
1159 wait_for_completion(&obd->obd_kobj_unregister);
1162 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1164 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1166 struct lprocfs_counter *cntr;
1167 unsigned int percpusize;
1169 unsigned long flags = 0;
1172 LASSERT(stats->ls_percpu[cpuid] == NULL);
1173 LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1175 percpusize = lprocfs_stats_counter_size(stats);
1176 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1177 if (stats->ls_percpu[cpuid]) {
1179 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1180 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1181 spin_lock_irqsave(&stats->ls_lock, flags);
1183 spin_lock(&stats->ls_lock);
1184 if (stats->ls_biggest_alloc_num <= cpuid)
1185 stats->ls_biggest_alloc_num = cpuid + 1;
1186 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1187 spin_unlock_irqrestore(&stats->ls_lock, flags);
1189 spin_unlock(&stats->ls_lock);
1192 /* initialize the ls_percpu[cpuid] non-zero counter */
1193 for (i = 0; i < stats->ls_num; ++i) {
1194 cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1195 cntr->lc_min = LC_MIN_INIT;
1201 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1202 enum lprocfs_stats_flags flags)
1204 struct lprocfs_stats *stats;
1205 unsigned int num_entry;
1206 unsigned int percpusize = 0;
1212 if (lprocfs_no_percpu_stats != 0)
1213 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1215 if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1218 num_entry = num_possible_cpus();
1220 /* alloc percpu pointers for all possible cpu slots */
1221 LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1225 stats->ls_num = num;
1226 stats->ls_flags = flags;
1227 spin_lock_init(&stats->ls_lock);
1229 /* alloc num of counter headers */
1230 CFS_ALLOC_PTR_ARRAY(stats->ls_cnt_header, stats->ls_num);
1231 if (!stats->ls_cnt_header)
1234 if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1235 /* contains only one set counters */
1236 percpusize = lprocfs_stats_counter_size(stats);
1237 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1238 if (!stats->ls_percpu[0])
1240 stats->ls_biggest_alloc_num = 1;
1241 } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1242 /* alloc all percpu data, currently only obd_memory use this */
1243 for (i = 0; i < num_entry; ++i)
1244 if (lprocfs_stats_alloc_one(stats, i) < 0)
1251 lprocfs_free_stats(&stats);
1254 EXPORT_SYMBOL(lprocfs_alloc_stats);
1256 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1258 struct lprocfs_stats *stats = *statsh;
1259 unsigned int num_entry;
1260 unsigned int percpusize;
1263 if (!stats || stats->ls_num == 0)
1267 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1270 num_entry = num_possible_cpus();
1272 percpusize = lprocfs_stats_counter_size(stats);
1273 for (i = 0; i < num_entry; i++)
1274 if (stats->ls_percpu[i])
1275 LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1276 if (stats->ls_cnt_header)
1277 CFS_FREE_PTR_ARRAY(stats->ls_cnt_header, stats->ls_num);
1278 LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1280 EXPORT_SYMBOL(lprocfs_free_stats);
1282 u64 lprocfs_stats_collector(struct lprocfs_stats *stats, int idx,
1283 enum lprocfs_fields_flags field)
1285 unsigned long flags = 0;
1286 unsigned int num_cpu;
1292 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1293 for (i = 0; i < num_cpu; i++) {
1294 struct lprocfs_counter *cntr;
1296 if (!stats->ls_percpu[i])
1299 cntr = lprocfs_stats_counter_get(stats, i, idx);
1300 ret += lprocfs_read_helper(cntr, &stats->ls_cnt_header[idx],
1301 stats->ls_flags, field);
1303 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1306 EXPORT_SYMBOL(lprocfs_stats_collector);
1308 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1310 struct lprocfs_counter *percpu_cntr;
1313 unsigned int num_entry;
1314 unsigned long flags = 0;
1316 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1318 for (i = 0; i < num_entry; i++) {
1319 if (!stats->ls_percpu[i])
1321 for (j = 0; j < stats->ls_num; j++) {
1322 percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1323 percpu_cntr->lc_count = 0;
1324 percpu_cntr->lc_min = LC_MIN_INIT;
1325 percpu_cntr->lc_max = 0;
1326 percpu_cntr->lc_sumsquare = 0;
1327 percpu_cntr->lc_sum = 0;
1328 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1329 percpu_cntr->lc_sum_irq = 0;
1333 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1335 EXPORT_SYMBOL(lprocfs_clear_stats);
1337 static ssize_t lprocfs_stats_seq_write(struct file *file,
1338 const char __user *buf,
1339 size_t len, loff_t *off)
1341 struct seq_file *seq = file->private_data;
1342 struct lprocfs_stats *stats = seq->private;
1344 lprocfs_clear_stats(stats);
1349 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1351 struct lprocfs_stats *stats = p->private;
1353 return (*pos < stats->ls_num) ? pos : NULL;
1356 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1360 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1364 return lprocfs_stats_seq_start(p, pos);
1367 /* seq file export of one lprocfs counter */
1368 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1370 struct lprocfs_stats *stats = p->private;
1371 struct lprocfs_counter_header *hdr;
1372 struct lprocfs_counter ctr;
1373 int idx = *(loff_t *)v;
1376 struct timespec64 now;
1378 ktime_get_real_ts64(&now);
1379 seq_printf(p, "%-25s %llu.%09lu secs.nsecs\n",
1380 "snapshot_time", (s64)now.tv_sec, now.tv_nsec);
1383 hdr = &stats->ls_cnt_header[idx];
1384 lprocfs_stats_collect(stats, idx, &ctr);
1386 if (ctr.lc_count == 0)
1389 seq_printf(p, "%-25s %lld samples [%s]", hdr->lc_name,
1390 ctr.lc_count, hdr->lc_units);
1392 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1393 seq_printf(p, " %lld %lld %lld",
1394 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1395 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1396 seq_printf(p, " %llu", ctr.lc_sumsquare);
1402 static const struct seq_operations lprocfs_stats_seq_sops = {
1403 .start = lprocfs_stats_seq_start,
1404 .stop = lprocfs_stats_seq_stop,
1405 .next = lprocfs_stats_seq_next,
1406 .show = lprocfs_stats_seq_show,
1409 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1411 struct seq_file *seq;
1414 rc = seq_open(file, &lprocfs_stats_seq_sops);
1417 seq = file->private_data;
1418 seq->private = inode->i_private ? inode->i_private : PDE_DATA(inode);
1422 static const struct file_operations lprocfs_stats_seq_fops = {
1423 .owner = THIS_MODULE,
1424 .open = lprocfs_stats_seq_open,
1426 .write = lprocfs_stats_seq_write,
1427 .llseek = seq_lseek,
1428 .release = lprocfs_seq_release,
1431 int ldebugfs_register_stats(struct dentry *parent, const char *name,
1432 struct lprocfs_stats *stats)
1434 struct dentry *entry;
1436 LASSERT(!IS_ERR_OR_NULL(parent));
1438 entry = debugfs_create_file(name, 0644, parent, stats,
1439 &lprocfs_stats_seq_fops);
1440 if (IS_ERR_OR_NULL(entry))
1441 return entry ? PTR_ERR(entry) : -ENOMEM;
1445 EXPORT_SYMBOL_GPL(ldebugfs_register_stats);
1447 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1448 struct lprocfs_stats *stats)
1450 struct proc_dir_entry *entry;
1451 LASSERT(root != NULL);
1453 entry = proc_create_data(name, 0644, root,
1454 &lprocfs_stats_seq_fops, stats);
1459 EXPORT_SYMBOL(lprocfs_register_stats);
1461 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1462 unsigned conf, const char *name, const char *units)
1464 struct lprocfs_counter_header *header;
1465 struct lprocfs_counter *percpu_cntr;
1466 unsigned long flags = 0;
1468 unsigned int num_cpu;
1470 LASSERT(stats != NULL);
1472 header = &stats->ls_cnt_header[index];
1473 LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1474 index, name, units);
1476 header->lc_config = conf;
1477 header->lc_name = name;
1478 header->lc_units = units;
1480 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1481 for (i = 0; i < num_cpu; ++i) {
1482 if (!stats->ls_percpu[i])
1484 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1485 percpu_cntr->lc_count = 0;
1486 percpu_cntr->lc_min = LC_MIN_INIT;
1487 percpu_cntr->lc_max = 0;
1488 percpu_cntr->lc_sumsquare = 0;
1489 percpu_cntr->lc_sum = 0;
1490 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1491 percpu_cntr->lc_sum_irq = 0;
1493 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1495 EXPORT_SYMBOL(lprocfs_counter_init);
1497 static const char * const mps_stats[] = {
1498 [LPROC_MD_CLOSE] = "close",
1499 [LPROC_MD_CREATE] = "create",
1500 [LPROC_MD_ENQUEUE] = "enqueue",
1501 [LPROC_MD_GETATTR] = "getattr",
1502 [LPROC_MD_INTENT_LOCK] = "intent_lock",
1503 [LPROC_MD_LINK] = "link",
1504 [LPROC_MD_RENAME] = "rename",
1505 [LPROC_MD_SETATTR] = "setattr",
1506 [LPROC_MD_FSYNC] = "fsync",
1507 [LPROC_MD_READ_PAGE] = "read_page",
1508 [LPROC_MD_UNLINK] = "unlink",
1509 [LPROC_MD_SETXATTR] = "setxattr",
1510 [LPROC_MD_GETXATTR] = "getxattr",
1511 [LPROC_MD_INTENT_GETATTR_ASYNC] = "intent_getattr_async",
1512 [LPROC_MD_REVALIDATE_LOCK] = "revalidate_lock",
1515 int lprocfs_alloc_md_stats(struct obd_device *obd,
1516 unsigned int num_private_stats)
1518 struct lprocfs_stats *stats;
1519 unsigned int num_stats;
1523 * TODO Ensure that this function is only used where
1524 * appropriate by adding an assertion to the effect that
1525 * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1526 * because mdt_procfs_init() uses this function to allocate
1527 * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1528 * mdt layer does not use the md_ops interface. This is
1529 * confusing and a waste of memory. See LU-2484.
1531 LASSERT(obd->obd_proc_entry != NULL);
1532 LASSERT(obd->obd_md_stats == NULL);
1534 num_stats = ARRAY_SIZE(mps_stats) + num_private_stats;
1535 stats = lprocfs_alloc_stats(num_stats, 0);
1539 for (i = 0; i < ARRAY_SIZE(mps_stats); i++) {
1540 lprocfs_counter_init(stats, i, 0, mps_stats[i], "reqs");
1541 if (!stats->ls_cnt_header[i].lc_name) {
1542 CERROR("Missing md_stat initializer md_op operation at offset %d. Aborting.\n",
1548 rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1550 lprocfs_free_stats(&stats);
1552 obd->obd_md_stats = stats;
1557 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1559 void lprocfs_free_md_stats(struct obd_device *obd)
1561 struct lprocfs_stats *stats = obd->obd_md_stats;
1564 obd->obd_md_stats = NULL;
1565 lprocfs_free_stats(&stats);
1568 EXPORT_SYMBOL(lprocfs_free_md_stats);
1570 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1572 lprocfs_counter_init(ldlm_stats,
1573 LDLM_ENQUEUE - LDLM_FIRST_OPC,
1574 0, "ldlm_enqueue", "reqs");
1575 lprocfs_counter_init(ldlm_stats,
1576 LDLM_CONVERT - LDLM_FIRST_OPC,
1577 0, "ldlm_convert", "reqs");
1578 lprocfs_counter_init(ldlm_stats,
1579 LDLM_CANCEL - LDLM_FIRST_OPC,
1580 0, "ldlm_cancel", "reqs");
1581 lprocfs_counter_init(ldlm_stats,
1582 LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1583 0, "ldlm_bl_callback", "reqs");
1584 lprocfs_counter_init(ldlm_stats,
1585 LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1586 0, "ldlm_cp_callback", "reqs");
1587 lprocfs_counter_init(ldlm_stats,
1588 LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1589 0, "ldlm_gl_callback", "reqs");
1591 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1593 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1594 struct lprocfs_counter_header *header,
1595 enum lprocfs_stats_flags flags,
1596 enum lprocfs_fields_flags field)
1604 case LPROCFS_FIELDS_FLAGS_CONFIG:
1605 ret = header->lc_config;
1607 case LPROCFS_FIELDS_FLAGS_SUM:
1609 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1610 ret += lc->lc_sum_irq;
1612 case LPROCFS_FIELDS_FLAGS_MIN:
1615 case LPROCFS_FIELDS_FLAGS_MAX:
1618 case LPROCFS_FIELDS_FLAGS_AVG:
1619 ret = (lc->lc_max - lc->lc_min) / 2;
1621 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1622 ret = lc->lc_sumsquare;
1624 case LPROCFS_FIELDS_FLAGS_COUNT:
1632 EXPORT_SYMBOL(lprocfs_read_helper);
1635 * string_to_size - convert ASCII string representing a numerical
1636 * value with optional units to 64-bit binary value
1638 * @size: The numerical value extract out of @buffer
1639 * @buffer: passed in string to parse
1640 * @count: length of the @buffer
1642 * This function returns a 64-bit binary value if @buffer contains a valid
1643 * numerical string. The string is parsed to 3 significant figures after
1644 * the decimal point. Support the string containing an optional units at
1645 * the end which can be base 2 or base 10 in value. If no units are given
1646 * the string is assumed to just a numerical value.
1648 * Returns: @count if the string is successfully parsed,
1649 * -errno on invalid input strings. Error values:
1651 * - ``-EINVAL``: @buffer is not a proper numerical string
1652 * - ``-EOVERFLOW``: results does not fit into 64 bits.
1653 * - ``-E2BIG ``: @buffer is not large
1655 int string_to_size(u64 *size, const char *buffer, size_t count)
1657 /* For string_get_size() it can support values above exabytes,
1658 * (ZiB, YiB) due to breaking the return value into a size and
1659 * bulk size to avoid 64 bit overflow. We don't break the size
1660 * up into block size units so we don't support ZiB or YiB.
1662 static const char *const units_10[] = {
1663 "kB", "MB", "GB", "TB", "PB", "EB"
1665 static const char *const units_2[] = {
1666 "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"
1668 static const char *const *const units_str[] = {
1669 [STRING_UNITS_2] = units_2,
1670 [STRING_UNITS_10] = units_10,
1672 static const unsigned int coeff[] = {
1673 [STRING_UNITS_10] = 1000,
1674 [STRING_UNITS_2] = 1024,
1676 enum string_size_units unit;
1677 u64 whole, blk_size = 1;
1678 char kernbuf[22], *end;
1683 if (count >= sizeof(kernbuf))
1687 /* 'iB' is used for based 2 numbers. If @buffer contains only a 'B'
1688 * or only numbers then we treat it as a direct number which doesn't
1689 * matter if its STRING_UNITS_2 or STRING_UNIT_10.
1691 unit = strstr(buffer, "iB") ? STRING_UNITS_2 : STRING_UNITS_10;
1692 i = unit == STRING_UNITS_2 ? ARRAY_SIZE(units_2) - 1 :
1693 ARRAY_SIZE(units_10) - 1;
1695 end = strstr(buffer, units_str[unit][i]);
1698 blk_size *= coeff[unit];
1704 /* as 'B' is a substring of all units, we need to handle it
1708 /* 'B' is only acceptable letter at this point */
1709 end = strchr(buffer, 'B');
1713 if (count - len > 2 ||
1714 (count - len == 2 && strcmp(end, "B\n") != 0))
1717 /* kstrtoull will error out if it has non digits */
1721 end = strchr(buffer, '.');
1723 /* need to limit 3 decimal places */
1724 char rem[4] = "000";
1731 /* limit to 3 decimal points */
1732 off = min_t(size_t, 3, strspn(end, "0123456789"));
1733 /* need to limit frac_d to a u32 */
1734 memcpy(rem, end, off);
1735 rc = kstrtoull(rem, 10, &frac);
1739 if (fls64(frac) + fls64(blk_size) - 1 > 64)
1747 snprintf(kernbuf, sizeof(kernbuf), "%.*s", (int)len, buffer);
1748 rc = kstrtoull(kernbuf, 10, &whole);
1752 if (whole != 0 && fls64(whole) + fls64(blk_size) - 1 > 64)
1755 *size += whole * blk_size;
1759 EXPORT_SYMBOL(string_to_size);
1762 * sysfs_memparse - parse a ASCII string to 64-bit binary value,
1763 * with optional units
1765 * @buffer: kernel pointer to input string
1766 * @count: number of bytes in the input @buffer
1767 * @val: (output) binary value returned to caller
1768 * @defunit: default unit suffix to use if none is provided
1770 * Parses a string into a number. The number stored at @buffer is
1771 * potentially suffixed with K, M, G, T, P, E. Besides these other
1772 * valid suffix units are shown in the string_to_size() function.
1773 * If the string lacks a suffix then the defunit is used. The defunit
1774 * should be given as a binary unit (e.g. MiB) as that is the standard
1775 * for tunables in Lustre. If no unit suffix is given (e.g. 'G'), then
1776 * it is assumed to be in binary units.
1778 * Returns: 0 on success or -errno on failure.
1780 int sysfs_memparse(const char *buffer, size_t count, u64 *val,
1781 const char *defunit)
1786 if (count >= sizeof(param))
1789 count = strlen(buffer);
1790 if (count && buffer[count - 1] == '\n')
1796 if (isalpha(buffer[count - 1])) {
1797 if (buffer[count - 1] != 'B') {
1798 scnprintf(param, sizeof(param), "%.*siB",
1799 (int)count, buffer);
1801 memcpy(param, buffer, sizeof(param));
1804 scnprintf(param, sizeof(param), "%.*s%s", (int)count,
1808 rc = string_to_size(val, param, strlen(param));
1809 return rc < 0 ? rc : 0;
1811 EXPORT_SYMBOL(sysfs_memparse);
1813 /* Obtains the conversion factor for the unit specified */
1814 static int get_mult(char unit, __u64 *mult)
1819 /* peta, tera, giga, mega, and kilo */
1840 /* some tests expect % to be accepted */
1854 * Ensures the numeric string is valid. The function provides the final
1855 * multiplier in the case a unit exists at the end of the string. It also
1856 * locates the start of the whole and fractional parts (if any). This
1857 * function modifies the string so kstrtoull can be used to parse both
1858 * the whole and fraction portions. This function also figures out
1859 * the base of the number.
1861 static int preprocess_numeric_str(char *buffer, __u64 *mult, __u64 def_mult,
1862 bool allow_units, char **whole, char **frac,
1865 bool hit_decimal = false;
1866 bool hit_unit = false;
1874 /* a hex string if it starts with "0x" */
1875 if (buffer[0] == '0' && tolower(buffer[1]) == 'x') {
1883 /* allow for a single new line before the null terminator */
1884 if (*buffer == '\n') {
1894 /* any chars after our unit indicates a malformed string */
1898 /* ensure we only hit one decimal */
1899 if (*buffer == '.') {
1903 /* if past start, there's a whole part */
1904 if (start != buffer)
1910 } else if (!isdigit(*buffer) &&
1911 !(*base == 16 && isxdigit(*buffer))) {
1913 /* if we allow units, attempt to get mult */
1915 rc = get_mult(*buffer, mult);
1919 /* string stops here, but keep processing */
1931 /* hit a decimal, make sure there's a fractional part */
1937 /* didn't hit a decimal, but may have a whole part */
1938 if (start != buffer && *start)
1942 /* malformed string if we didn't get anything */
1943 if (!*frac && !*whole)
1950 * Parses a numeric string which can contain a whole and fraction portion
1951 * into a __u64. Accepts a multiplier to apply to the value parsed. Also
1952 * allows the string to have a unit at the end. The function handles
1953 * wrapping of the final unsigned value.
1955 static int str_to_u64_parse(char *buffer, unsigned long count,
1956 __u64 *val, __u64 def_mult, bool allow_units)
1960 unsigned int frac_d = 1;
1961 __u64 wrap_indicator = ULLONG_MAX;
1966 unsigned int base = 10;
1968 rc = preprocess_numeric_str(buffer, &mult, def_mult, allow_units,
1969 &strwhole, &strfrac, &base);
1979 /* the multiplier limits how large the value can be */
1980 wrap_indicator = div64_u64(wrap_indicator, mult);
1983 rc = kstrtoull(strwhole, base, &whole);
1987 if (whole > wrap_indicator)
1994 if (strlen(strfrac) > 10)
1997 rc = kstrtoull(strfrac, base, &frac);
2001 /* determine power of fractional portion */
2007 /* fractional portion is too large to perform calculation */
2008 if (frac > wrap_indicator)
2012 do_div(frac, frac_d);
2015 /* check that the sum of whole and fraction fits in u64 */
2016 if (whole > (ULLONG_MAX - frac))
2019 *val = whole + frac;
2025 * This function parses numeric/hex strings into __s64. It accepts a multiplier
2026 * which will apply to the value parsed. It also can allow the string to
2027 * have a unit as the last character. The function handles overflow/underflow
2028 * of the signed integer.
2030 int lu_str_to_s64(char *buffer, unsigned long count, __s64 *val, char defunit)
2034 unsigned int offset = 0;
2035 int signed sign = 1;
2036 __u64 max = LLONG_MAX;
2039 if (defunit != '1') {
2040 rc = get_mult(defunit, &mult);
2045 /* keep track of our sign */
2046 if (*buffer == '-') {
2049 /* equivalent to max = -LLONG_MIN, avoids overflow */
2053 rc = str_to_u64_parse(buffer + offset, count - offset,
2058 /* check for overflow/underflow */
2062 *val = (__s64)tmp * sign;
2066 EXPORT_SYMBOL(lu_str_to_s64);
2068 /* identical to s64 version, but does not handle overflow */
2069 static int str_to_u64_internal(const char __user *buffer, unsigned long count,
2070 __u64 *val, __u64 def_mult, bool allow_units)
2073 unsigned int offset = 0;
2076 if (count > (sizeof(kernbuf) - 1))
2079 if (copy_from_user(kernbuf, buffer, count))
2082 kernbuf[count] = '\0';
2084 rc = str_to_u64_parse(kernbuf + offset, count - offset,
2085 val, def_mult, allow_units);
2092 * Convert a user string into a signed 64 bit number. This function produces
2093 * an error when the value parsed from the string times multiplier underflows or
2094 * overflows. This function only accepts strings that contains digits, an
2095 * optional decimal, and a char representing a unit at the end. If a unit is
2096 * specified in the string, the multiplier provided by the caller is ignored.
2097 * This function can also accept hexadecimal strings which are prefixed with
2100 * \param[in] buffer string consisting of numbers, a decimal, and a unit
2101 * \param[in] count buffer length
2102 * \param[in] val if successful, the value represented by the string
2103 * \param[in] defunit default unit if string doesn't contain one
2105 * \retval 0 on success
2106 * \retval negative number on error
2108 int lprocfs_str_with_units_to_s64(const char __user *buffer,
2109 unsigned long count, __s64 *val, char defunit)
2113 if (count > (sizeof(kernbuf) - 1))
2116 if (copy_from_user(kernbuf, buffer, count))
2119 kernbuf[count] = '\0';
2121 return lu_str_to_s64(kernbuf, count, val, defunit);
2123 EXPORT_SYMBOL(lprocfs_str_with_units_to_s64);
2125 /* identical to s64 version above, but does not handle overflow */
2126 int lprocfs_str_with_units_to_u64(const char __user *buffer,
2127 unsigned long count, __u64 *val, char defunit)
2132 if (defunit != '1') {
2133 rc = get_mult(defunit, &mult);
2138 return str_to_u64_internal(buffer, count, val, mult, true);
2140 EXPORT_SYMBOL(lprocfs_str_with_units_to_u64);
2142 char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
2151 if (!memcmp(s1, s2, l2))
2157 EXPORT_SYMBOL(lprocfs_strnstr);
2160 * Find the string \a name in the input \a buffer, and return a pointer to the
2161 * value immediately following \a name, reducing \a count appropriately.
2162 * If \a name is not found the original \a buffer is returned.
2164 char *lprocfs_find_named_value(const char *buffer, const char *name,
2168 size_t buflen = *count;
2170 /* there is no strnstr() in rhel5 and ubuntu kernels */
2171 val = lprocfs_strnstr(buffer, name, buflen);
2173 return (char *)buffer;
2175 val += strlen(name); /* skip prefix */
2176 while (val < buffer + buflen && isspace(*val)) /* skip separator */
2180 while (val < buffer + buflen && isalnum(*val)) {
2185 return val - *count;
2187 EXPORT_SYMBOL(lprocfs_find_named_value);
2189 int ldebugfs_seq_create(struct dentry *parent, const char *name, umode_t mode,
2190 const struct file_operations *seq_fops, void *data)
2192 struct dentry *entry;
2194 /* Disallow secretly (un)writable entries. */
2195 LASSERT((!seq_fops->write) == (!(mode & 0222)));
2197 entry = debugfs_create_file(name, mode, parent, data, seq_fops);
2198 if (IS_ERR_OR_NULL(entry))
2199 return entry ? PTR_ERR(entry) : -ENOMEM;
2203 EXPORT_SYMBOL_GPL(ldebugfs_seq_create);
2205 int lprocfs_seq_create(struct proc_dir_entry *parent,
2208 const struct file_operations *seq_fops,
2211 struct proc_dir_entry *entry;
2214 /* Disallow secretly (un)writable entries. */
2215 LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
2217 entry = proc_create_data(name, mode, parent, seq_fops, data);
2224 EXPORT_SYMBOL(lprocfs_seq_create);
2226 int lprocfs_obd_seq_create(struct obd_device *obd,
2229 const struct file_operations *seq_fops,
2232 return lprocfs_seq_create(obd->obd_proc_entry, name,
2233 mode, seq_fops, data);
2235 EXPORT_SYMBOL(lprocfs_obd_seq_create);
2237 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
2239 if (value >= OBD_HIST_MAX)
2240 value = OBD_HIST_MAX - 1;
2242 spin_lock(&oh->oh_lock);
2243 oh->oh_buckets[value]++;
2244 spin_unlock(&oh->oh_lock);
2246 EXPORT_SYMBOL(lprocfs_oh_tally);
2248 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
2250 unsigned int val = 0;
2252 if (likely(value != 0))
2253 val = min(fls(value - 1), OBD_HIST_MAX);
2255 lprocfs_oh_tally(oh, val);
2257 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
2259 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
2261 unsigned long ret = 0;
2264 for (i = 0; i < OBD_HIST_MAX; i++)
2265 ret += oh->oh_buckets[i];
2268 EXPORT_SYMBOL(lprocfs_oh_sum);
2270 void lprocfs_oh_clear(struct obd_histogram *oh)
2272 spin_lock(&oh->oh_lock);
2273 memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
2274 spin_unlock(&oh->oh_lock);
2276 EXPORT_SYMBOL(lprocfs_oh_clear);
2278 ssize_t lustre_attr_show(struct kobject *kobj,
2279 struct attribute *attr, char *buf)
2281 struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
2283 return a->show ? a->show(kobj, attr, buf) : 0;
2285 EXPORT_SYMBOL_GPL(lustre_attr_show);
2287 ssize_t lustre_attr_store(struct kobject *kobj, struct attribute *attr,
2288 const char *buf, size_t len)
2290 struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
2292 return a->store ? a->store(kobj, attr, buf, len) : len;
2294 EXPORT_SYMBOL_GPL(lustre_attr_store);
2296 const struct sysfs_ops lustre_sysfs_ops = {
2297 .show = lustre_attr_show,
2298 .store = lustre_attr_store,
2300 EXPORT_SYMBOL_GPL(lustre_sysfs_ops);
2302 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
2304 struct obd_device *obd = data;
2305 struct client_obd *cli = &obd->u.cli;
2307 spin_lock(&cli->cl_loi_list_lock);
2308 seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
2309 spin_unlock(&cli->cl_loi_list_lock);
2312 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
2314 ssize_t lprocfs_obd_max_pages_per_rpc_seq_write(struct file *file,
2315 const char __user *buffer,
2316 size_t count, loff_t *off)
2318 struct seq_file *m = file->private_data;
2319 struct obd_device *obd = m->private;
2320 struct client_obd *cli = &obd->u.cli;
2321 struct obd_import *imp;
2322 struct obd_connect_data *ocd;
2327 if (count > sizeof(kernbuf) - 1)
2330 if (copy_from_user(kernbuf, buffer, count))
2333 kernbuf[count] = '\0';
2335 rc = sysfs_memparse(kernbuf, count, &val, "B");
2339 /* if the max_pages is specified in bytes, convert to pages */
2340 if (val >= ONE_MB_BRW_SIZE)
2343 with_imp_locked(obd, imp, rc) {
2344 ocd = &imp->imp_connect_data;
2345 chunk_mask = ~((1 << (cli->cl_chunkbits - PAGE_SHIFT)) - 1);
2346 /* max_pages_per_rpc must be chunk aligned */
2347 val = (val + ~chunk_mask) & chunk_mask;
2348 if (val == 0 || (ocd->ocd_brw_size != 0 &&
2349 val > ocd->ocd_brw_size >> PAGE_SHIFT)) {
2352 spin_lock(&cli->cl_loi_list_lock);
2353 cli->cl_max_pages_per_rpc = val;
2354 client_adjust_max_dirty(cli);
2355 spin_unlock(&cli->cl_loi_list_lock);
2361 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_write);
2363 ssize_t short_io_bytes_show(struct kobject *kobj, struct attribute *attr,
2366 struct obd_device *obd = container_of(kobj, struct obd_device,
2368 struct client_obd *cli = &obd->u.cli;
2371 spin_lock(&cli->cl_loi_list_lock);
2372 rc = sprintf(buf, "%d\n", cli->cl_max_short_io_bytes);
2373 spin_unlock(&cli->cl_loi_list_lock);
2376 EXPORT_SYMBOL(short_io_bytes_show);
2378 /* Used to catch people who think they're specifying pages. */
2379 #define MIN_SHORT_IO_BYTES 64U
2381 ssize_t short_io_bytes_store(struct kobject *kobj, struct attribute *attr,
2382 const char *buffer, size_t count)
2384 struct obd_device *obd = container_of(kobj, struct obd_device,
2386 struct client_obd *cli = &obd->u.cli;
2390 if (strcmp(buffer, "-1") == 0) {
2391 val = OBD_DEF_SHORT_IO_BYTES;
2393 rc = sysfs_memparse(buffer, count, &val, "B");
2398 if (val && (val < MIN_SHORT_IO_BYTES || val > LNET_MTU))
2399 GOTO(out, rc = -ERANGE);
2403 spin_lock(&cli->cl_loi_list_lock);
2404 cli->cl_max_short_io_bytes = min_t(u64, val, OST_MAX_SHORT_IO_BYTES);
2405 spin_unlock(&cli->cl_loi_list_lock);
2410 EXPORT_SYMBOL(short_io_bytes_store);
2412 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2413 struct root_squash_info *squash, char *name)
2416 char kernbuf[64], *tmp, *errmsg;
2417 unsigned long uid, gid;
2420 if (count >= sizeof(kernbuf)) {
2421 errmsg = "string too long";
2422 GOTO(failed_noprint, rc = -EINVAL);
2424 if (copy_from_user(kernbuf, buffer, count)) {
2425 errmsg = "bad address";
2426 GOTO(failed_noprint, rc = -EFAULT);
2428 kernbuf[count] = '\0';
2430 /* look for uid gid separator */
2431 tmp = strchr(kernbuf, ':');
2433 errmsg = "needs uid:gid format";
2434 GOTO(failed, rc = -EINVAL);
2440 if (kstrtoul(kernbuf, 0, &uid) != 0) {
2442 GOTO(failed, rc = -EINVAL);
2446 if (kstrtoul(tmp, 0, &gid) != 0) {
2448 GOTO(failed, rc = -EINVAL);
2451 squash->rsi_uid = uid;
2452 squash->rsi_gid = gid;
2454 LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2455 name, squash->rsi_uid, squash->rsi_gid);
2463 CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2464 name, kernbuf, errmsg, rc);
2467 CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2471 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2474 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2475 struct root_squash_info *squash, char *name)
2478 char *kernbuf = NULL;
2485 errmsg = "string too long";
2486 GOTO(failed, rc = -EINVAL);
2489 OBD_ALLOC(kernbuf, count + 1);
2491 errmsg = "no memory";
2492 GOTO(failed, rc = -ENOMEM);
2494 if (copy_from_user(kernbuf, buffer, count)) {
2495 errmsg = "bad address";
2496 GOTO(failed, rc = -EFAULT);
2498 kernbuf[count] = '\0';
2500 if (count > 0 && kernbuf[count - 1] == '\n')
2503 if ((len == 4 && strncmp(kernbuf, "NONE", len) == 0) ||
2504 (len == 5 && strncmp(kernbuf, "clear", len) == 0)) {
2505 /* empty string is special case */
2506 spin_lock(&squash->rsi_lock);
2507 if (!list_empty(&squash->rsi_nosquash_nids))
2508 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2509 spin_unlock(&squash->rsi_lock);
2510 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2511 OBD_FREE(kernbuf, count + 1);
2515 if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2516 errmsg = "can't parse";
2517 GOTO(failed, rc = -EINVAL);
2519 LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2521 OBD_FREE(kernbuf, count + 1);
2524 spin_lock(&squash->rsi_lock);
2525 if (!list_empty(&squash->rsi_nosquash_nids))
2526 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2527 list_splice(&tmp, &squash->rsi_nosquash_nids);
2528 spin_unlock(&squash->rsi_lock);
2534 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2535 name, kernbuf, errmsg, rc);
2536 OBD_FREE(kernbuf, count + 1);
2538 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2543 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2545 #endif /* CONFIG_PROC_FS*/