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/
31 * lustre/obdclass/lprocfs_status.c
33 * Author: Hariharan Thantry <thantry@users.sourceforge.net>
36 #define DEBUG_SUBSYSTEM S_CLASS
38 #include <obd_class.h>
39 #include <lprocfs_status.h>
43 static int lprocfs_no_percpu_stats = 0;
44 module_param(lprocfs_no_percpu_stats, int, 0644);
45 MODULE_PARM_DESC(lprocfs_no_percpu_stats, "Do not alloc percpu data for lprocfs stats");
47 #define MAX_STRING_SIZE 128
49 int lprocfs_single_release(struct inode *inode, struct file *file)
51 return single_release(inode, file);
53 EXPORT_SYMBOL(lprocfs_single_release);
55 int lprocfs_seq_release(struct inode *inode, struct file *file)
57 return seq_release(inode, file);
59 EXPORT_SYMBOL(lprocfs_seq_release);
61 static umode_t default_mode(const struct proc_ops *ops)
73 struct proc_dir_entry *
74 lprocfs_add_simple(struct proc_dir_entry *root, char *name,
75 void *data, const struct proc_ops *fops)
77 struct proc_dir_entry *proc;
80 if (!root || !name || !fops)
81 return ERR_PTR(-EINVAL);
83 mode = default_mode(fops);
84 proc = proc_create_data(name, mode, root, fops, data);
86 CERROR("LprocFS: No memory to create /proc entry %s\n",
88 return ERR_PTR(-ENOMEM);
92 EXPORT_SYMBOL(lprocfs_add_simple);
94 struct proc_dir_entry *lprocfs_add_symlink(const char *name,
95 struct proc_dir_entry *parent,
96 const char *format, ...)
98 struct proc_dir_entry *entry;
102 if (!parent || !format)
105 OBD_ALLOC_WAIT(dest, MAX_STRING_SIZE + 1);
109 va_start(ap, format);
110 vsnprintf(dest, MAX_STRING_SIZE, format, ap);
113 entry = proc_symlink(name, parent, dest);
115 CERROR("LprocFS: Could not create symbolic link from "
116 "%s to %s\n", name, dest);
118 OBD_FREE(dest, MAX_STRING_SIZE + 1);
121 EXPORT_SYMBOL(lprocfs_add_symlink);
123 static const struct file_operations ldebugfs_empty_ops = { };
125 void ldebugfs_add_vars(struct dentry *parent, struct ldebugfs_vars *list,
128 if (IS_ERR_OR_NULL(parent) || IS_ERR_OR_NULL(list))
134 if (list->proc_mode != 0000) {
135 mode = list->proc_mode;
136 } else if (list->fops) {
137 if (list->fops->read)
139 if (list->fops->write)
142 debugfs_create_file(list->name, mode, parent,
144 list->fops ? : &ldebugfs_empty_ops);
148 EXPORT_SYMBOL_GPL(ldebugfs_add_vars);
150 static const struct proc_ops lprocfs_empty_ops = { };
155 * \param root [in] The parent proc entry on which new entry will be added.
156 * \param list [in] Array of proc entries to be added.
157 * \param data [in] The argument to be passed when entries read/write routines
158 * are called through /proc file.
160 * \retval 0 on success
164 lprocfs_add_vars(struct proc_dir_entry *root, struct lprocfs_vars *list,
171 struct proc_dir_entry *proc;
175 mode = list->proc_mode;
177 mode = default_mode(list->fops);
178 proc = proc_create_data(list->name, mode, root,
179 list->fops ?: &lprocfs_empty_ops,
187 EXPORT_SYMBOL(lprocfs_add_vars);
189 void lprocfs_remove(struct proc_dir_entry **rooth)
194 EXPORT_SYMBOL(lprocfs_remove);
196 void lprocfs_remove_proc_entry(const char *name, struct proc_dir_entry *parent)
198 LASSERT(parent != NULL);
199 remove_proc_entry(name, parent);
201 EXPORT_SYMBOL(lprocfs_remove_proc_entry);
203 struct proc_dir_entry *
204 lprocfs_register(const char *name, struct proc_dir_entry *parent,
205 struct lprocfs_vars *list, void *data)
207 struct proc_dir_entry *newchild;
209 newchild = proc_mkdir(name, parent);
211 return ERR_PTR(-ENOMEM);
214 int rc = lprocfs_add_vars(newchild, list, data);
216 lprocfs_remove(&newchild);
222 EXPORT_SYMBOL(lprocfs_register);
224 /* Generic callbacks */
225 int lprocfs_uuid_seq_show(struct seq_file *m, void *data)
227 struct obd_device *obd = data;
229 LASSERT(obd != NULL);
230 seq_printf(m, "%s\n", obd->obd_uuid.uuid);
233 EXPORT_SYMBOL(lprocfs_uuid_seq_show);
235 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
238 struct obd_device *obd = container_of(kobj, struct obd_device,
241 return sprintf(buf, "%s\n", obd->obd_uuid.uuid);
243 LUSTRE_RO_ATTR(uuid);
245 static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
248 struct obd_device *obd = container_of(kobj, struct obd_device,
250 struct obd_statfs osfs;
253 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
254 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
257 return sprintf(buf, "%u\n", osfs.os_bsize);
261 LUSTRE_RO_ATTR(blocksize);
263 static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
266 struct obd_device *obd = container_of(kobj, struct obd_device,
268 struct obd_statfs osfs;
271 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
272 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
275 u32 blk_size = osfs.os_bsize >> 10;
276 u64 result = osfs.os_blocks;
278 result *= rounddown_pow_of_two(blk_size ?: 1);
279 return sprintf(buf, "%llu\n", result);
284 LUSTRE_RO_ATTR(kbytestotal);
286 static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
289 struct obd_device *obd = container_of(kobj, struct obd_device,
291 struct obd_statfs osfs;
294 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
295 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
298 u32 blk_size = osfs.os_bsize >> 10;
299 u64 result = osfs.os_bfree;
301 while (blk_size >>= 1)
304 return sprintf(buf, "%llu\n", result);
309 LUSTRE_RO_ATTR(kbytesfree);
311 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
314 struct obd_device *obd = container_of(kobj, struct obd_device,
316 struct obd_statfs osfs;
319 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
320 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
323 u32 blk_size = osfs.os_bsize >> 10;
324 u64 result = osfs.os_bavail;
326 while (blk_size >>= 1)
329 return sprintf(buf, "%llu\n", result);
334 LUSTRE_RO_ATTR(kbytesavail);
336 static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
339 struct obd_device *obd = container_of(kobj, struct obd_device,
341 struct obd_statfs osfs;
344 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
345 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
348 return sprintf(buf, "%llu\n", osfs.os_files);
352 LUSTRE_RO_ATTR(filestotal);
354 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
357 struct obd_device *obd = container_of(kobj, struct obd_device,
359 struct obd_statfs osfs;
362 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
363 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
366 return sprintf(buf, "%llu\n", osfs.os_ffree);
370 LUSTRE_RO_ATTR(filesfree);
372 ssize_t conn_uuid_show(struct kobject *kobj, struct attribute *attr, char *buf)
374 struct obd_device *obd = container_of(kobj, struct obd_device,
376 struct obd_import *imp;
377 struct ptlrpc_connection *conn;
380 with_imp_locked(obd, imp, count) {
381 conn = imp->imp_connection;
383 count = sprintf(buf, "%s\n", conn->c_remote_uuid.uuid);
385 count = sprintf(buf, "%s\n", "<none>");
390 EXPORT_SYMBOL(conn_uuid_show);
392 int lprocfs_server_uuid_seq_show(struct seq_file *m, void *data)
394 struct obd_device *obd = data;
395 struct obd_import *imp;
396 const char *imp_state_name = NULL;
399 LASSERT(obd != NULL);
400 with_imp_locked(obd, imp, rc) {
401 imp_state_name = ptlrpc_import_state_name(imp->imp_state);
402 seq_printf(m, "%s\t%s%s\n", obd2cli_tgt(obd), imp_state_name,
403 imp->imp_deactive ? "\tDEACTIVATED" : "");
408 EXPORT_SYMBOL(lprocfs_server_uuid_seq_show);
410 /** add up per-cpu counters */
413 * Lock statistics structure for access, possibly only on this CPU.
415 * The statistics struct may be allocated with per-CPU structures for
416 * efficient concurrent update (usually only on server-wide stats), or
417 * as a single global struct (e.g. for per-client or per-job statistics),
418 * so the required locking depends on the type of structure allocated.
420 * For per-CPU statistics, pin the thread to the current cpuid so that
421 * will only access the statistics for that CPU. If the stats structure
422 * for the current CPU has not been allocated (or previously freed),
423 * allocate it now. The per-CPU statistics do not need locking since
424 * the thread is pinned to the CPU during update.
426 * For global statistics, lock the stats structure to prevent concurrent update.
428 * \param[in] stats statistics structure to lock
429 * \param[in] opc type of operation:
430 * LPROCFS_GET_SMP_ID: "lock" and return current CPU index
431 * for incrementing statistics for that CPU
432 * LPROCFS_GET_NUM_CPU: "lock" and return number of used
433 * CPU indices to iterate over all indices
434 * \param[out] flags CPU interrupt saved state for IRQ-safe locking
436 * \retval cpuid of current thread or number of allocated structs
437 * \retval negative on error (only for opc LPROCFS_GET_SMP_ID + per-CPU stats)
439 int lprocfs_stats_lock(struct lprocfs_stats *stats,
440 enum lprocfs_stats_lock_ops opc,
441 unsigned long *flags)
443 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
444 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
445 spin_lock_irqsave(&stats->ls_lock, *flags);
447 spin_lock(&stats->ls_lock);
448 return opc == LPROCFS_GET_NUM_CPU ? 1 : 0;
452 case LPROCFS_GET_SMP_ID: {
453 unsigned int cpuid = get_cpu();
455 if (unlikely(!stats->ls_percpu[cpuid])) {
456 int rc = lprocfs_stats_alloc_one(stats, cpuid);
465 case LPROCFS_GET_NUM_CPU:
466 return stats->ls_biggest_alloc_num;
473 * Unlock statistics structure after access.
475 * Unlock the lock acquired via lprocfs_stats_lock() for global statistics,
476 * or unpin this thread from the current cpuid for per-CPU statistics.
478 * This function must be called using the same arguments as used when calling
479 * lprocfs_stats_lock() so that the correct operation can be performed.
481 * \param[in] stats statistics structure to unlock
482 * \param[in] opc type of operation (current cpuid or number of structs)
483 * \param[in] flags CPU interrupt saved state for IRQ-safe locking
485 void lprocfs_stats_unlock(struct lprocfs_stats *stats,
486 enum lprocfs_stats_lock_ops opc,
487 unsigned long *flags)
489 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
490 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
491 spin_unlock_irqrestore(&stats->ls_lock, *flags);
493 spin_unlock(&stats->ls_lock);
494 } else if (opc == LPROCFS_GET_SMP_ID) {
499 /** add up per-cpu counters */
500 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
501 struct lprocfs_counter *cnt)
503 unsigned int num_entry;
504 struct lprocfs_counter *percpu_cntr;
506 unsigned long flags = 0;
508 memset(cnt, 0, sizeof(*cnt));
511 /* set count to 1 to avoid divide-by-zero errs in callers */
516 cnt->lc_min = LC_MIN_INIT;
518 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
520 for (i = 0; i < num_entry; i++) {
521 if (!stats->ls_percpu[i])
523 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
525 cnt->lc_count += percpu_cntr->lc_count;
526 cnt->lc_sum += percpu_cntr->lc_sum;
527 if (percpu_cntr->lc_min < cnt->lc_min)
528 cnt->lc_min = percpu_cntr->lc_min;
529 if (percpu_cntr->lc_max > cnt->lc_max)
530 cnt->lc_max = percpu_cntr->lc_max;
531 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
534 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
537 static void obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
541 if (imp->imp_obd->obd_no_recov) {
542 seq_printf(m, "no_recov");
546 flag2str(imp, invalid);
547 flag2str(imp, deactive);
548 flag2str(imp, replayable);
549 flag2str(imp, delayed_recovery);
550 flag2str(imp, vbr_failed);
551 flag2str(imp, pingable);
552 flag2str(imp, resend_replay);
553 flag2str(imp, no_pinger_recover);
554 flag2str(imp, connect_tried);
557 static const char *const obd_connect_names[] = {
576 "remote_client_by_force",
585 "mds_mds_connection",
588 "alt_checksum_algorithm",
624 "file_secctx", /* 0x01 */
625 "lockaheadv2", /* 0x02 */
626 "dir_migrate", /* 0x04 */
627 "sum_statfs", /* 0x08 */
628 "overstriping", /* 0x10 */
631 "lock_convert", /* 0x80 */
632 "archive_id_array", /* 0x100 */
633 "increasing_xid", /* 0x200 */
634 "selinux_policy", /* 0x400 */
637 "crush", /* 0x2000 */
638 "async_discard", /* 0x4000 */
639 "client_encryption", /* 0x8000 */
640 "fidmap", /* 0x10000 */
641 "getattr_pfid", /* 0x20000 */
642 "lseek", /* 0x40000 */
643 "dom_lvb", /* 0x80000 */
647 void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, __u64 flags2,
654 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
656 seq_printf(m, "%s%s",
657 first ? "" : sep, obd_connect_names[i]);
662 if (flags & ~(mask - 1)) {
663 seq_printf(m, "%sunknown_%#llx",
664 first ? "" : sep, flags & ~(mask - 1));
668 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
671 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
673 seq_printf(m, "%s%s",
674 first ? "" : sep, obd_connect_names[i]);
679 if (flags2 & ~(mask - 1)) {
680 seq_printf(m, "%sunknown2_%#llx",
681 first ? "" : sep, flags2 & ~(mask - 1));
685 EXPORT_SYMBOL(obd_connect_seq_flags2str);
687 int obd_connect_flags2str(char *page, int count, __u64 flags, __u64 flags2,
693 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
695 ret += snprintf(page + ret, count - ret, "%s%s",
696 ret ? sep : "", obd_connect_names[i]);
699 if (flags & ~(mask - 1))
700 ret += snprintf(page + ret, count - ret,
702 ret ? sep : "", flags & ~(mask - 1));
704 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
707 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
709 ret += snprintf(page + ret, count - ret, "%s%s",
710 ret ? sep : "", obd_connect_names[i]);
713 if (flags2 & ~(mask - 1))
714 ret += snprintf(page + ret, count - ret,
716 ret ? sep : "", flags2 & ~(mask - 1));
720 EXPORT_SYMBOL(obd_connect_flags2str);
723 obd_connect_data_seqprint(struct seq_file *m, struct obd_connect_data *ocd)
727 LASSERT(ocd != NULL);
728 flags = ocd->ocd_connect_flags;
730 seq_printf(m, " connect_data:\n"
733 ocd->ocd_connect_flags,
735 if (flags & OBD_CONNECT_VERSION)
736 seq_printf(m, " target_version: %u.%u.%u.%u\n",
737 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
738 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
739 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
740 OBD_OCD_VERSION_FIX(ocd->ocd_version));
741 if (flags & OBD_CONNECT_MDS)
742 seq_printf(m, " mdt_index: %d\n", ocd->ocd_group);
743 if (flags & OBD_CONNECT_GRANT)
744 seq_printf(m, " initial_grant: %d\n", ocd->ocd_grant);
745 if (flags & OBD_CONNECT_INDEX)
746 seq_printf(m, " target_index: %u\n", ocd->ocd_index);
747 if (flags & OBD_CONNECT_BRW_SIZE)
748 seq_printf(m, " max_brw_size: %d\n", ocd->ocd_brw_size);
749 if (flags & OBD_CONNECT_IBITS)
750 seq_printf(m, " ibits_known: %#llx\n",
751 ocd->ocd_ibits_known);
752 if (flags & OBD_CONNECT_GRANT_PARAM)
753 seq_printf(m, " grant_block_size: %d\n"
754 " grant_inode_size: %d\n"
755 " grant_max_extent_size: %d\n"
756 " grant_extent_tax: %d\n",
757 1 << ocd->ocd_grant_blkbits,
758 1 << ocd->ocd_grant_inobits,
759 ocd->ocd_grant_max_blks << ocd->ocd_grant_blkbits,
760 ocd->ocd_grant_tax_kb << 10);
761 if (flags & OBD_CONNECT_TRANSNO)
762 seq_printf(m, " first_transno: %#llx\n",
764 if (flags & OBD_CONNECT_CKSUM)
765 seq_printf(m, " cksum_types: %#x\n",
766 ocd->ocd_cksum_types);
767 if (flags & OBD_CONNECT_MAX_EASIZE)
768 seq_printf(m, " max_easize: %d\n", ocd->ocd_max_easize);
769 if (flags & OBD_CONNECT_MAXBYTES)
770 seq_printf(m, " max_object_bytes: %llu\n",
772 if (flags & OBD_CONNECT_MULTIMODRPCS)
773 seq_printf(m, " max_mod_rpcs: %hu\n",
774 ocd->ocd_maxmodrpcs);
777 static void lprocfs_import_seq_show_locked(struct seq_file *m,
778 struct obd_device *obd,
779 struct obd_import *imp)
781 char nidstr[LNET_NIDSTR_SIZE];
782 struct lprocfs_counter ret;
783 struct lprocfs_counter_header *header;
784 struct obd_import_conn *conn;
785 struct obd_connect_data *ocd;
790 ocd = &imp->imp_connect_data;
792 seq_printf(m, "import:\n"
796 " connect_flags: [ ",
799 ptlrpc_import_state_name(imp->imp_state));
800 obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
801 imp->imp_connect_data.ocd_connect_flags2,
803 seq_printf(m, " ]\n");
804 obd_connect_data_seqprint(m, ocd);
805 seq_printf(m, " import_flags: [ ");
806 obd_import_flags2str(imp, m);
810 " failover_nids: [ ");
811 spin_lock(&imp->imp_lock);
813 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
814 libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
815 nidstr, sizeof(nidstr));
816 seq_printf(m, "%s%s", j ? ", " : "", nidstr);
819 if (imp->imp_connection)
820 libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
821 nidstr, sizeof(nidstr));
823 strncpy(nidstr, "<none>", sizeof(nidstr));
825 " current_connection: %s\n"
826 " connection_attempts: %u\n"
828 " in-progress_invalidations: %u\n"
833 atomic_read(&imp->imp_inval_count),
834 ktime_get_real_seconds() - imp->imp_last_reply_time);
835 spin_unlock(&imp->imp_lock);
837 if (!obd->obd_svc_stats)
840 header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
841 lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
842 if (ret.lc_count != 0)
843 ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
846 seq_printf(m, " rpcs:\n"
848 " unregistering: %u\n"
850 " avg_waittime: %llu %s\n",
851 atomic_read(&imp->imp_inflight),
852 atomic_read(&imp->imp_unregistering),
853 atomic_read(&imp->imp_timeouts),
854 ret.lc_sum, header->lc_units);
857 for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
858 if (imp->imp_at.iat_portal[j] == 0)
860 k = max_t(unsigned int, k,
861 at_get(&imp->imp_at.iat_service_estimate[j]));
863 seq_printf(m, " service_estimates:\n"
864 " services: %u sec\n"
865 " network: %d sec\n",
867 at_get(&imp->imp_at.iat_net_latency));
869 seq_printf(m, " transactions:\n"
870 " last_replay: %llu\n"
871 " peer_committed: %llu\n"
872 " last_checked: %llu\n",
873 imp->imp_last_replay_transno,
874 imp->imp_peer_committed_transno,
875 imp->imp_last_transno_checked);
878 for (rw = 0; rw <= 1; rw++) {
879 lprocfs_stats_collect(obd->obd_svc_stats,
880 PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
882 if (ret.lc_sum > 0 && ret.lc_count > 0) {
883 ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
884 seq_printf(m, " %s_data_averages:\n"
885 " bytes_per_rpc: %llu\n",
886 rw ? "write" : "read",
890 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
891 header = &obd->obd_svc_stats->ls_cnt_header[j];
892 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
893 if (ret.lc_sum > 0 && ret.lc_count != 0) {
894 ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
895 seq_printf(m, " %s_per_rpc: %llu\n",
896 header->lc_units, ret.lc_sum);
899 seq_printf(m, " MB_per_sec: %u.%.02u\n",
900 k / j, (100 * k / j) % 100);
905 int lprocfs_import_seq_show(struct seq_file *m, void *data)
907 struct obd_device *obd = (struct obd_device *)data;
908 struct obd_import *imp;
911 LASSERT(obd != NULL);
912 with_imp_locked(obd, imp, rv)
913 lprocfs_import_seq_show_locked(m, obd, imp);
916 EXPORT_SYMBOL(lprocfs_import_seq_show);
918 int lprocfs_state_seq_show(struct seq_file *m, void *data)
920 struct obd_device *obd = (struct obd_device *)data;
921 struct obd_import *imp;
925 LASSERT(obd != NULL);
926 with_imp_locked(obd, imp, rc) {
927 seq_printf(m, "current_state: %s\n",
928 ptlrpc_import_state_name(imp->imp_state));
929 seq_printf(m, "state_history:\n");
930 k = imp->imp_state_hist_idx;
931 for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
932 struct import_state_hist *ish =
933 &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
934 if (ish->ish_state == 0)
936 seq_printf(m, " - [ %lld, %s ]\n", (s64)ish->ish_time,
937 ptlrpc_import_state_name(ish->ish_state));
943 EXPORT_SYMBOL(lprocfs_state_seq_show);
945 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
948 for (i = 0; i < AT_BINS; i++)
949 seq_printf(m, "%3u ", at->at_hist[i]);
953 EXPORT_SYMBOL(lprocfs_at_hist_helper);
955 /* See also ptlrpc_lprocfs_timeouts_show_seq */
956 static void lprocfs_timeouts_seq_show_locked(struct seq_file *m,
957 struct obd_device *obd,
958 struct obd_import *imp)
960 timeout_t cur_timeout, worst_timeout;
961 time64_t now, worst_timestamp;
964 LASSERT(obd != NULL);
966 now = ktime_get_real_seconds();
968 /* Some network health info for kicks */
969 seq_printf(m, "%-10s : %lld, %llds ago\n",
970 "last reply", (s64)imp->imp_last_reply_time,
971 (s64)(now - imp->imp_last_reply_time));
973 cur_timeout = at_get(&imp->imp_at.iat_net_latency);
974 worst_timeout = imp->imp_at.iat_net_latency.at_worst_timeout_ever;
975 worst_timestamp = imp->imp_at.iat_net_latency.at_worst_timestamp;
976 seq_printf(m, "%-10s : cur %3u worst %3u (at %lld, %llds ago) ",
977 "network", cur_timeout, worst_timeout, worst_timestamp,
978 now - worst_timestamp);
979 lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
981 for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
982 struct adaptive_timeout *service_est;
984 if (imp->imp_at.iat_portal[i] == 0)
987 service_est = &imp->imp_at.iat_service_estimate[i];
988 cur_timeout = at_get(service_est);
989 worst_timeout = service_est->at_worst_timeout_ever;
990 worst_timestamp = service_est->at_worst_timestamp;
991 seq_printf(m, "portal %-2d : cur %3u worst %3u (at %lld, %llds ago) ",
992 imp->imp_at.iat_portal[i], cur_timeout,
993 worst_timeout, worst_timestamp,
994 now - worst_timestamp);
995 lprocfs_at_hist_helper(m, service_est);
999 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
1001 struct obd_device *obd = (struct obd_device *)data;
1002 struct obd_import *imp;
1005 with_imp_locked(obd, imp, rc)
1006 lprocfs_timeouts_seq_show_locked(m, obd, imp);
1009 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
1011 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
1013 struct obd_device *obd = data;
1016 struct obd_import *imp;
1019 with_imp_locked(obd, imp, rc) {
1020 flags = imp->imp_connect_data.ocd_connect_flags;
1021 flags2 = imp->imp_connect_data.ocd_connect_flags2;
1022 seq_printf(m, "flags=%#llx\n", flags);
1023 seq_printf(m, "flags2=%#llx\n", flags2);
1024 obd_connect_seq_flags2str(m, flags, flags2, "\n");
1025 seq_printf(m, "\n");
1030 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
1032 static const struct attribute *obd_def_uuid_attrs[] = {
1033 &lustre_attr_uuid.attr,
1037 static const struct attribute *obd_def_attrs[] = {
1038 &lustre_attr_blocksize.attr,
1039 &lustre_attr_kbytestotal.attr,
1040 &lustre_attr_kbytesfree.attr,
1041 &lustre_attr_kbytesavail.attr,
1042 &lustre_attr_filestotal.attr,
1043 &lustre_attr_filesfree.attr,
1044 &lustre_attr_uuid.attr,
1048 static void obd_sysfs_release(struct kobject *kobj)
1050 struct obd_device *obd = container_of(kobj, struct obd_device,
1053 complete(&obd->obd_kobj_unregister);
1056 int lprocfs_obd_setup(struct obd_device *obd, bool uuid_only)
1058 struct ldebugfs_vars *debugfs_vars = NULL;
1061 if (!obd || obd->obd_magic != OBD_DEVICE_MAGIC)
1064 rc = kobject_set_name(&obd->obd_kset.kobj, "%s", obd->obd_name);
1068 obd->obd_ktype.sysfs_ops = &lustre_sysfs_ops;
1069 obd->obd_ktype.release = obd_sysfs_release;
1071 obd->obd_kset.kobj.parent = &obd->obd_type->typ_kobj;
1072 obd->obd_kset.kobj.ktype = &obd->obd_ktype;
1073 init_completion(&obd->obd_kobj_unregister);
1074 rc = kset_register(&obd->obd_kset);
1079 obd->obd_attrs = obd_def_uuid_attrs;
1081 obd->obd_attrs = obd_def_attrs;
1083 rc = sysfs_create_files(&obd->obd_kset.kobj, obd->obd_attrs);
1085 kset_unregister(&obd->obd_kset);
1089 if (!obd->obd_type->typ_procroot)
1090 debugfs_vars = obd->obd_debugfs_vars;
1091 obd->obd_debugfs_entry = debugfs_create_dir(
1092 obd->obd_name, obd->obd_type->typ_debugfs_entry);
1093 ldebugfs_add_vars(obd->obd_debugfs_entry, debugfs_vars, obd);
1095 if (obd->obd_proc_entry || !obd->obd_type->typ_procroot)
1096 GOTO(already_registered, rc);
1098 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1099 obd->obd_type->typ_procroot,
1100 obd->obd_vars, obd);
1101 if (IS_ERR(obd->obd_proc_entry)) {
1102 rc = PTR_ERR(obd->obd_proc_entry);
1103 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1104 obd->obd_proc_entry = NULL;
1106 debugfs_remove_recursive(obd->obd_debugfs_entry);
1107 obd->obd_debugfs_entry = NULL;
1109 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1110 obd->obd_attrs = NULL;
1111 kset_unregister(&obd->obd_kset);
1117 EXPORT_SYMBOL(lprocfs_obd_setup);
1119 int lprocfs_obd_cleanup(struct obd_device *obd)
1124 if (obd->obd_proc_exports_entry) {
1125 /* Should be no exports left */
1126 lprocfs_remove(&obd->obd_proc_exports_entry);
1127 obd->obd_proc_exports_entry = NULL;
1130 if (obd->obd_proc_entry) {
1131 lprocfs_remove(&obd->obd_proc_entry);
1132 obd->obd_proc_entry = NULL;
1135 debugfs_remove_recursive(obd->obd_debugfs_entry);
1136 obd->obd_debugfs_entry = NULL;
1138 /* obd device never allocated a kset */
1139 if (!obd->obd_kset.kobj.state_initialized)
1142 if (obd->obd_attrs) {
1143 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1144 obd->obd_attrs = NULL;
1147 kset_unregister(&obd->obd_kset);
1148 wait_for_completion(&obd->obd_kobj_unregister);
1151 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1153 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1155 struct lprocfs_counter *cntr;
1156 unsigned int percpusize;
1158 unsigned long flags = 0;
1161 LASSERT(stats->ls_percpu[cpuid] == NULL);
1162 LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1164 percpusize = lprocfs_stats_counter_size(stats);
1165 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1166 if (stats->ls_percpu[cpuid]) {
1168 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1169 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1170 spin_lock_irqsave(&stats->ls_lock, flags);
1172 spin_lock(&stats->ls_lock);
1173 if (stats->ls_biggest_alloc_num <= cpuid)
1174 stats->ls_biggest_alloc_num = cpuid + 1;
1175 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1176 spin_unlock_irqrestore(&stats->ls_lock, flags);
1178 spin_unlock(&stats->ls_lock);
1181 /* initialize the ls_percpu[cpuid] non-zero counter */
1182 for (i = 0; i < stats->ls_num; ++i) {
1183 cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1184 cntr->lc_min = LC_MIN_INIT;
1190 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1191 enum lprocfs_stats_flags flags)
1193 struct lprocfs_stats *stats;
1194 unsigned int num_entry;
1195 unsigned int percpusize = 0;
1201 if (lprocfs_no_percpu_stats != 0)
1202 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1204 if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1207 num_entry = num_possible_cpus();
1209 /* alloc percpu pointers for all possible cpu slots */
1210 LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1214 stats->ls_num = num;
1215 stats->ls_flags = flags;
1216 spin_lock_init(&stats->ls_lock);
1218 /* alloc num of counter headers */
1219 CFS_ALLOC_PTR_ARRAY(stats->ls_cnt_header, stats->ls_num);
1220 if (!stats->ls_cnt_header)
1223 if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1224 /* contains only one set counters */
1225 percpusize = lprocfs_stats_counter_size(stats);
1226 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1227 if (!stats->ls_percpu[0])
1229 stats->ls_biggest_alloc_num = 1;
1230 } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1231 /* alloc all percpu data, currently only obd_memory use this */
1232 for (i = 0; i < num_entry; ++i)
1233 if (lprocfs_stats_alloc_one(stats, i) < 0)
1240 lprocfs_free_stats(&stats);
1243 EXPORT_SYMBOL(lprocfs_alloc_stats);
1245 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1247 struct lprocfs_stats *stats = *statsh;
1248 unsigned int num_entry;
1249 unsigned int percpusize;
1252 if (!stats || stats->ls_num == 0)
1256 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1259 num_entry = num_possible_cpus();
1261 percpusize = lprocfs_stats_counter_size(stats);
1262 for (i = 0; i < num_entry; i++)
1263 if (stats->ls_percpu[i])
1264 LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1265 if (stats->ls_cnt_header)
1266 CFS_FREE_PTR_ARRAY(stats->ls_cnt_header, stats->ls_num);
1267 LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1269 EXPORT_SYMBOL(lprocfs_free_stats);
1271 u64 lprocfs_stats_collector(struct lprocfs_stats *stats, int idx,
1272 enum lprocfs_fields_flags field)
1274 unsigned long flags = 0;
1275 unsigned int num_cpu;
1281 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1282 for (i = 0; i < num_cpu; i++) {
1283 struct lprocfs_counter *cntr;
1285 if (!stats->ls_percpu[i])
1288 cntr = lprocfs_stats_counter_get(stats, i, idx);
1289 ret += lprocfs_read_helper(cntr, &stats->ls_cnt_header[idx],
1290 stats->ls_flags, field);
1292 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1295 EXPORT_SYMBOL(lprocfs_stats_collector);
1297 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1299 struct lprocfs_counter *percpu_cntr;
1302 unsigned int num_entry;
1303 unsigned long flags = 0;
1305 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1307 for (i = 0; i < num_entry; i++) {
1308 if (!stats->ls_percpu[i])
1310 for (j = 0; j < stats->ls_num; j++) {
1311 percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1312 percpu_cntr->lc_count = 0;
1313 percpu_cntr->lc_min = LC_MIN_INIT;
1314 percpu_cntr->lc_max = 0;
1315 percpu_cntr->lc_sumsquare = 0;
1316 percpu_cntr->lc_sum = 0;
1317 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1318 percpu_cntr->lc_sum_irq = 0;
1322 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1324 EXPORT_SYMBOL(lprocfs_clear_stats);
1326 static ssize_t lprocfs_stats_seq_write(struct file *file,
1327 const char __user *buf,
1328 size_t len, loff_t *off)
1330 struct seq_file *seq = file->private_data;
1331 struct lprocfs_stats *stats = seq->private;
1333 lprocfs_clear_stats(stats);
1338 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1340 struct lprocfs_stats *stats = p->private;
1342 return (*pos < stats->ls_num) ? pos : NULL;
1345 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1349 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1353 return lprocfs_stats_seq_start(p, pos);
1356 /* seq file export of one lprocfs counter */
1357 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1359 struct lprocfs_stats *stats = p->private;
1360 struct lprocfs_counter_header *hdr;
1361 struct lprocfs_counter ctr;
1362 int idx = *(loff_t *)v;
1365 struct timespec64 now;
1367 ktime_get_real_ts64(&now);
1368 seq_printf(p, "%-25s %llu.%09lu secs.nsecs\n",
1369 "snapshot_time", (s64)now.tv_sec, now.tv_nsec);
1372 hdr = &stats->ls_cnt_header[idx];
1373 lprocfs_stats_collect(stats, idx, &ctr);
1375 if (ctr.lc_count == 0)
1378 seq_printf(p, "%-25s %lld samples [%s]", hdr->lc_name,
1379 ctr.lc_count, hdr->lc_units);
1381 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1382 seq_printf(p, " %lld %lld %lld",
1383 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1384 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1385 seq_printf(p, " %llu", ctr.lc_sumsquare);
1391 static const struct seq_operations lprocfs_stats_seq_sops = {
1392 .start = lprocfs_stats_seq_start,
1393 .stop = lprocfs_stats_seq_stop,
1394 .next = lprocfs_stats_seq_next,
1395 .show = lprocfs_stats_seq_show,
1398 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1400 struct seq_file *seq;
1403 rc = seq_open(file, &lprocfs_stats_seq_sops);
1406 seq = file->private_data;
1407 seq->private = inode->i_private ? inode->i_private : PDE_DATA(inode);
1411 const struct file_operations ldebugfs_stats_seq_fops = {
1412 .owner = THIS_MODULE,
1413 .open = lprocfs_stats_seq_open,
1415 .write = lprocfs_stats_seq_write,
1416 .llseek = seq_lseek,
1417 .release = lprocfs_seq_release,
1419 EXPORT_SYMBOL(ldebugfs_stats_seq_fops);
1421 static const struct proc_ops lprocfs_stats_seq_fops = {
1422 PROC_OWNER(THIS_MODULE)
1423 .proc_open = lprocfs_stats_seq_open,
1424 .proc_read = seq_read,
1425 .proc_write = lprocfs_stats_seq_write,
1426 .proc_lseek = seq_lseek,
1427 .proc_release = lprocfs_seq_release,
1430 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1431 struct lprocfs_stats *stats)
1433 struct proc_dir_entry *entry;
1434 LASSERT(root != NULL);
1436 entry = proc_create_data(name, 0644, root,
1437 &lprocfs_stats_seq_fops, stats);
1442 EXPORT_SYMBOL(lprocfs_register_stats);
1444 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1445 unsigned conf, const char *name, const char *units)
1447 struct lprocfs_counter_header *header;
1448 struct lprocfs_counter *percpu_cntr;
1449 unsigned long flags = 0;
1451 unsigned int num_cpu;
1453 LASSERT(stats != NULL);
1455 header = &stats->ls_cnt_header[index];
1456 LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1457 index, name, units);
1459 header->lc_config = conf;
1460 header->lc_name = name;
1461 header->lc_units = units;
1463 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1464 for (i = 0; i < num_cpu; ++i) {
1465 if (!stats->ls_percpu[i])
1467 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1468 percpu_cntr->lc_count = 0;
1469 percpu_cntr->lc_min = LC_MIN_INIT;
1470 percpu_cntr->lc_max = 0;
1471 percpu_cntr->lc_sumsquare = 0;
1472 percpu_cntr->lc_sum = 0;
1473 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1474 percpu_cntr->lc_sum_irq = 0;
1476 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1478 EXPORT_SYMBOL(lprocfs_counter_init);
1480 static const char * const mps_stats[] = {
1481 [LPROC_MD_CLOSE] = "close",
1482 [LPROC_MD_CREATE] = "create",
1483 [LPROC_MD_ENQUEUE] = "enqueue",
1484 [LPROC_MD_GETATTR] = "getattr",
1485 [LPROC_MD_INTENT_LOCK] = "intent_lock",
1486 [LPROC_MD_LINK] = "link",
1487 [LPROC_MD_RENAME] = "rename",
1488 [LPROC_MD_SETATTR] = "setattr",
1489 [LPROC_MD_FSYNC] = "fsync",
1490 [LPROC_MD_READ_PAGE] = "read_page",
1491 [LPROC_MD_UNLINK] = "unlink",
1492 [LPROC_MD_SETXATTR] = "setxattr",
1493 [LPROC_MD_GETXATTR] = "getxattr",
1494 [LPROC_MD_INTENT_GETATTR_ASYNC] = "intent_getattr_async",
1495 [LPROC_MD_REVALIDATE_LOCK] = "revalidate_lock",
1498 int lprocfs_alloc_md_stats(struct obd_device *obd,
1499 unsigned int num_private_stats)
1501 struct lprocfs_stats *stats;
1502 unsigned int num_stats;
1506 * TODO Ensure that this function is only used where
1507 * appropriate by adding an assertion to the effect that
1508 * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1509 * because mdt_procfs_init() uses this function to allocate
1510 * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1511 * mdt layer does not use the md_ops interface. This is
1512 * confusing and a waste of memory. See LU-2484.
1514 LASSERT(obd->obd_proc_entry != NULL);
1515 LASSERT(obd->obd_md_stats == NULL);
1517 num_stats = ARRAY_SIZE(mps_stats) + num_private_stats;
1518 stats = lprocfs_alloc_stats(num_stats, 0);
1522 for (i = 0; i < ARRAY_SIZE(mps_stats); i++) {
1523 lprocfs_counter_init(stats, i, 0, mps_stats[i], "reqs");
1524 if (!stats->ls_cnt_header[i].lc_name) {
1525 CERROR("Missing md_stat initializer md_op operation at offset %d. Aborting.\n",
1531 rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1533 lprocfs_free_stats(&stats);
1535 obd->obd_md_stats = stats;
1540 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1542 void lprocfs_free_md_stats(struct obd_device *obd)
1544 struct lprocfs_stats *stats = obd->obd_md_stats;
1547 obd->obd_md_stats = NULL;
1548 lprocfs_free_stats(&stats);
1551 EXPORT_SYMBOL(lprocfs_free_md_stats);
1553 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1555 lprocfs_counter_init(ldlm_stats,
1556 LDLM_ENQUEUE - LDLM_FIRST_OPC,
1557 0, "ldlm_enqueue", "reqs");
1558 lprocfs_counter_init(ldlm_stats,
1559 LDLM_CONVERT - LDLM_FIRST_OPC,
1560 0, "ldlm_convert", "reqs");
1561 lprocfs_counter_init(ldlm_stats,
1562 LDLM_CANCEL - LDLM_FIRST_OPC,
1563 0, "ldlm_cancel", "reqs");
1564 lprocfs_counter_init(ldlm_stats,
1565 LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1566 0, "ldlm_bl_callback", "reqs");
1567 lprocfs_counter_init(ldlm_stats,
1568 LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1569 0, "ldlm_cp_callback", "reqs");
1570 lprocfs_counter_init(ldlm_stats,
1571 LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1572 0, "ldlm_gl_callback", "reqs");
1574 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1576 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1577 struct lprocfs_counter_header *header,
1578 enum lprocfs_stats_flags flags,
1579 enum lprocfs_fields_flags field)
1587 case LPROCFS_FIELDS_FLAGS_CONFIG:
1588 ret = header->lc_config;
1590 case LPROCFS_FIELDS_FLAGS_SUM:
1592 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1593 ret += lc->lc_sum_irq;
1595 case LPROCFS_FIELDS_FLAGS_MIN:
1598 case LPROCFS_FIELDS_FLAGS_MAX:
1601 case LPROCFS_FIELDS_FLAGS_AVG:
1602 ret = (lc->lc_max - lc->lc_min) / 2;
1604 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1605 ret = lc->lc_sumsquare;
1607 case LPROCFS_FIELDS_FLAGS_COUNT:
1615 EXPORT_SYMBOL(lprocfs_read_helper);
1618 * string_to_size - convert ASCII string representing a numerical
1619 * value with optional units to 64-bit binary value
1621 * @size: The numerical value extract out of @buffer
1622 * @buffer: passed in string to parse
1623 * @count: length of the @buffer
1625 * This function returns a 64-bit binary value if @buffer contains a valid
1626 * numerical string. The string is parsed to 3 significant figures after
1627 * the decimal point. Support the string containing an optional units at
1628 * the end which can be base 2 or base 10 in value. If no units are given
1629 * the string is assumed to just a numerical value.
1631 * Returns: @count if the string is successfully parsed,
1632 * -errno on invalid input strings. Error values:
1634 * - ``-EINVAL``: @buffer is not a proper numerical string
1635 * - ``-EOVERFLOW``: results does not fit into 64 bits.
1636 * - ``-E2BIG ``: @buffer is too large (not a valid number)
1638 int string_to_size(u64 *size, const char *buffer, size_t count)
1640 /* For string_get_size() it can support values above exabytes,
1641 * (ZiB, YiB) due to breaking the return value into a size and
1642 * bulk size to avoid 64 bit overflow. We don't break the size
1643 * up into block size units so we don't support ZiB or YiB.
1645 static const char *const units_10[] = {
1646 "kB", "MB", "GB", "TB", "PB", "EB",
1648 static const char *const units_2[] = {
1649 "K", "M", "G", "T", "P", "E",
1651 static const char *const *const units_str[] = {
1652 [STRING_UNITS_2] = units_2,
1653 [STRING_UNITS_10] = units_10,
1655 static const unsigned int coeff[] = {
1656 [STRING_UNITS_10] = 1000,
1657 [STRING_UNITS_2] = 1024,
1659 enum string_size_units unit = STRING_UNITS_2;
1660 u64 whole, blk_size = 1;
1661 char kernbuf[22], *end;
1666 if (count >= sizeof(kernbuf)) {
1667 CERROR("count %zd > buffer %zd\n", count, sizeof(kernbuf));
1672 /* The "iB" suffix is optionally allowed for indicating base-2 numbers.
1673 * If suffix is only "B" and not "iB" then we treat it as base-10.
1675 end = strstr(buffer, "B");
1676 if (end && *(end - 1) != 'i')
1677 unit = STRING_UNITS_10;
1679 i = unit == STRING_UNITS_2 ? ARRAY_SIZE(units_2) - 1 :
1680 ARRAY_SIZE(units_10) - 1;
1682 end = strnstr(buffer, units_str[unit][i], count);
1685 blk_size *= coeff[unit];
1691 /* as 'B' is a substring of all units, we need to handle it
1695 /* 'B' is only acceptable letter at this point */
1696 end = strnchr(buffer, count, 'B');
1700 if (count - len > 2 ||
1701 (count - len == 2 && strcmp(end, "B\n") != 0)) {
1702 CDEBUG(D_INFO, "unknown suffix '%s'\n", buffer);
1706 /* kstrtoull will error out if it has non digits */
1710 end = strnchr(buffer, count, '.');
1712 /* need to limit 3 decimal places */
1713 char rem[4] = "000";
1720 /* limit to 3 decimal points */
1721 off = min_t(size_t, 3, strspn(end, "0123456789"));
1722 /* need to limit frac_d to a u32 */
1723 memcpy(rem, end, off);
1724 rc = kstrtoull(rem, 10, &frac);
1728 if (fls64(frac) + fls64(blk_size) - 1 > 64)
1736 snprintf(kernbuf, sizeof(kernbuf), "%.*s", (int)len, buffer);
1737 rc = kstrtoull(kernbuf, 10, &whole);
1741 if (whole != 0 && fls64(whole) + fls64(blk_size) - 1 > 64)
1744 *size += whole * blk_size;
1748 EXPORT_SYMBOL(string_to_size);
1751 * sysfs_memparse - parse a ASCII string to 64-bit binary value,
1752 * with optional units
1754 * @buffer: kernel pointer to input string
1755 * @count: number of bytes in the input @buffer
1756 * @val: (output) binary value returned to caller
1757 * @defunit: default unit suffix to use if none is provided
1759 * Parses a string into a number. The number stored at @buffer is
1760 * potentially suffixed with K, M, G, T, P, E. Besides these other
1761 * valid suffix units are shown in the string_to_size() function.
1762 * If the string lacks a suffix then the defunit is used. The defunit
1763 * should be given as a binary unit (e.g. MiB) as that is the standard
1764 * for tunables in Lustre. If no unit suffix is given (e.g. 'G'), then
1765 * it is assumed to be in binary units.
1767 * Returns: 0 on success or -errno on failure.
1769 int sysfs_memparse(const char *buffer, size_t count, u64 *val,
1770 const char *defunit)
1772 const char *param = buffer;
1776 count = strlen(buffer);
1777 while (count > 0 && isspace(buffer[count - 1]))
1783 /* If there isn't already a unit on this value, append @defunit.
1784 * Units of 'B' don't affect the value, so don't bother adding.
1786 if (!isalpha(buffer[count - 1]) && defunit[0] != 'B') {
1787 if (count + 3 >= sizeof(tmp_buf)) {
1788 CERROR("count %zd > size %zd\n", count, sizeof(param));
1792 scnprintf(tmp_buf, sizeof(tmp_buf), "%.*s%s", (int)count,
1795 count = strlen(param);
1798 rc = string_to_size(val, param, count);
1800 return rc < 0 ? rc : 0;
1802 EXPORT_SYMBOL(sysfs_memparse);
1804 char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1813 if (!memcmp(s1, s2, l2))
1819 EXPORT_SYMBOL(lprocfs_strnstr);
1822 * Find the string \a name in the input \a buffer, and return a pointer to the
1823 * value immediately following \a name, reducing \a count appropriately.
1824 * If \a name is not found the original \a buffer is returned.
1826 char *lprocfs_find_named_value(const char *buffer, const char *name,
1830 size_t buflen = *count;
1832 /* there is no strnstr() in rhel5 and ubuntu kernels */
1833 val = lprocfs_strnstr(buffer, name, buflen);
1835 return (char *)buffer;
1837 val += strlen(name); /* skip prefix */
1838 while (val < buffer + buflen && isspace(*val)) /* skip separator */
1842 while (val < buffer + buflen && isalnum(*val)) {
1847 return val - *count;
1849 EXPORT_SYMBOL(lprocfs_find_named_value);
1851 int lprocfs_seq_create(struct proc_dir_entry *parent,
1854 const struct proc_ops *seq_fops,
1857 struct proc_dir_entry *entry;
1860 /* Disallow secretly (un)writable entries. */
1861 LASSERT(!seq_fops->proc_write == !(mode & 0222));
1863 entry = proc_create_data(name, mode, parent, seq_fops, data);
1870 EXPORT_SYMBOL(lprocfs_seq_create);
1872 int lprocfs_obd_seq_create(struct obd_device *obd,
1875 const struct proc_ops *seq_fops,
1878 return lprocfs_seq_create(obd->obd_proc_entry, name,
1879 mode, seq_fops, data);
1881 EXPORT_SYMBOL(lprocfs_obd_seq_create);
1883 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
1885 if (value >= OBD_HIST_MAX)
1886 value = OBD_HIST_MAX - 1;
1888 spin_lock(&oh->oh_lock);
1889 oh->oh_buckets[value]++;
1890 spin_unlock(&oh->oh_lock);
1892 EXPORT_SYMBOL(lprocfs_oh_tally);
1894 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
1896 unsigned int val = 0;
1898 if (likely(value != 0))
1899 val = min(fls(value - 1), OBD_HIST_MAX);
1901 lprocfs_oh_tally(oh, val);
1903 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
1905 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
1907 unsigned long ret = 0;
1910 for (i = 0; i < OBD_HIST_MAX; i++)
1911 ret += oh->oh_buckets[i];
1914 EXPORT_SYMBOL(lprocfs_oh_sum);
1916 void lprocfs_oh_clear(struct obd_histogram *oh)
1918 spin_lock(&oh->oh_lock);
1919 memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
1920 spin_unlock(&oh->oh_lock);
1922 EXPORT_SYMBOL(lprocfs_oh_clear);
1924 ssize_t lustre_attr_show(struct kobject *kobj,
1925 struct attribute *attr, char *buf)
1927 struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1929 return a->show ? a->show(kobj, attr, buf) : 0;
1931 EXPORT_SYMBOL_GPL(lustre_attr_show);
1933 ssize_t lustre_attr_store(struct kobject *kobj, struct attribute *attr,
1934 const char *buf, size_t len)
1936 struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1938 return a->store ? a->store(kobj, attr, buf, len) : len;
1940 EXPORT_SYMBOL_GPL(lustre_attr_store);
1942 const struct sysfs_ops lustre_sysfs_ops = {
1943 .show = lustre_attr_show,
1944 .store = lustre_attr_store,
1946 EXPORT_SYMBOL_GPL(lustre_sysfs_ops);
1948 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
1950 struct obd_device *obd = data;
1951 struct client_obd *cli = &obd->u.cli;
1953 spin_lock(&cli->cl_loi_list_lock);
1954 seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
1955 spin_unlock(&cli->cl_loi_list_lock);
1958 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
1960 ssize_t lprocfs_obd_max_pages_per_rpc_seq_write(struct file *file,
1961 const char __user *buffer,
1962 size_t count, loff_t *off)
1964 struct seq_file *m = file->private_data;
1965 struct obd_device *obd = m->private;
1966 struct client_obd *cli = &obd->u.cli;
1967 struct obd_import *imp;
1968 struct obd_connect_data *ocd;
1973 if (count > sizeof(kernbuf) - 1)
1976 if (copy_from_user(kernbuf, buffer, count))
1979 kernbuf[count] = '\0';
1981 rc = sysfs_memparse(kernbuf, count, &val, "B");
1985 /* if the max_pages is specified in bytes, convert to pages */
1986 if (val >= ONE_MB_BRW_SIZE)
1989 with_imp_locked(obd, imp, rc) {
1990 ocd = &imp->imp_connect_data;
1991 chunk_mask = ~((1 << (cli->cl_chunkbits - PAGE_SHIFT)) - 1);
1992 /* max_pages_per_rpc must be chunk aligned */
1993 val = (val + ~chunk_mask) & chunk_mask;
1994 if (val == 0 || (ocd->ocd_brw_size != 0 &&
1995 val > ocd->ocd_brw_size >> PAGE_SHIFT)) {
1998 spin_lock(&cli->cl_loi_list_lock);
1999 cli->cl_max_pages_per_rpc = val;
2000 client_adjust_max_dirty(cli);
2001 spin_unlock(&cli->cl_loi_list_lock);
2007 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_write);
2009 ssize_t short_io_bytes_show(struct kobject *kobj, struct attribute *attr,
2012 struct obd_device *obd = container_of(kobj, struct obd_device,
2014 struct client_obd *cli = &obd->u.cli;
2017 spin_lock(&cli->cl_loi_list_lock);
2018 rc = sprintf(buf, "%d\n", cli->cl_max_short_io_bytes);
2019 spin_unlock(&cli->cl_loi_list_lock);
2022 EXPORT_SYMBOL(short_io_bytes_show);
2024 /* Used to catch people who think they're specifying pages. */
2025 #define MIN_SHORT_IO_BYTES 64U
2027 ssize_t short_io_bytes_store(struct kobject *kobj, struct attribute *attr,
2028 const char *buffer, size_t count)
2030 struct obd_device *obd = container_of(kobj, struct obd_device,
2032 struct client_obd *cli = &obd->u.cli;
2036 if (strcmp(buffer, "-1") == 0) {
2037 val = OBD_DEF_SHORT_IO_BYTES;
2039 rc = sysfs_memparse(buffer, count, &val, "B");
2044 if (val && (val < MIN_SHORT_IO_BYTES || val > LNET_MTU))
2045 GOTO(out, rc = -ERANGE);
2049 spin_lock(&cli->cl_loi_list_lock);
2050 cli->cl_max_short_io_bytes = min_t(u64, val, OST_MAX_SHORT_IO_BYTES);
2051 spin_unlock(&cli->cl_loi_list_lock);
2056 EXPORT_SYMBOL(short_io_bytes_store);
2058 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2059 struct root_squash_info *squash, char *name)
2062 char kernbuf[64], *tmp, *errmsg;
2063 unsigned long uid, gid;
2066 if (count >= sizeof(kernbuf)) {
2067 errmsg = "string too long";
2068 GOTO(failed_noprint, rc = -EINVAL);
2070 if (copy_from_user(kernbuf, buffer, count)) {
2071 errmsg = "bad address";
2072 GOTO(failed_noprint, rc = -EFAULT);
2074 kernbuf[count] = '\0';
2076 /* look for uid gid separator */
2077 tmp = strchr(kernbuf, ':');
2079 errmsg = "needs uid:gid format";
2080 GOTO(failed, rc = -EINVAL);
2086 if (kstrtoul(kernbuf, 0, &uid) != 0) {
2088 GOTO(failed, rc = -EINVAL);
2092 if (kstrtoul(tmp, 0, &gid) != 0) {
2094 GOTO(failed, rc = -EINVAL);
2097 squash->rsi_uid = uid;
2098 squash->rsi_gid = gid;
2100 LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2101 name, squash->rsi_uid, squash->rsi_gid);
2109 CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2110 name, kernbuf, errmsg, rc);
2113 CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2117 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2120 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2121 struct root_squash_info *squash, char *name)
2124 char *kernbuf = NULL;
2131 errmsg = "string too long";
2132 GOTO(failed, rc = -EINVAL);
2135 OBD_ALLOC(kernbuf, count + 1);
2137 errmsg = "no memory";
2138 GOTO(failed, rc = -ENOMEM);
2140 if (copy_from_user(kernbuf, buffer, count)) {
2141 errmsg = "bad address";
2142 GOTO(failed, rc = -EFAULT);
2144 kernbuf[count] = '\0';
2146 if (count > 0 && kernbuf[count - 1] == '\n')
2149 if ((len == 4 && strncmp(kernbuf, "NONE", len) == 0) ||
2150 (len == 5 && strncmp(kernbuf, "clear", len) == 0)) {
2151 /* empty string is special case */
2152 spin_lock(&squash->rsi_lock);
2153 if (!list_empty(&squash->rsi_nosquash_nids))
2154 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2155 spin_unlock(&squash->rsi_lock);
2156 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2157 OBD_FREE(kernbuf, count + 1);
2161 if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2162 errmsg = "can't parse";
2163 GOTO(failed, rc = -EINVAL);
2165 LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2167 OBD_FREE(kernbuf, count + 1);
2170 spin_lock(&squash->rsi_lock);
2171 if (!list_empty(&squash->rsi_nosquash_nids))
2172 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2173 list_splice(&tmp, &squash->rsi_nosquash_nids);
2174 spin_unlock(&squash->rsi_lock);
2180 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2181 name, kernbuf, errmsg, rc);
2182 OBD_FREE(kernbuf, count + 1);
2184 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2189 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2191 #endif /* CONFIG_PROC_FS*/