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 */
644 "reply_mbits", /* 0x100000 */
648 void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, __u64 flags2,
655 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
657 seq_printf(m, "%s%s",
658 first ? "" : sep, obd_connect_names[i]);
663 if (flags & ~(mask - 1)) {
664 seq_printf(m, "%sunknown_%#llx",
665 first ? "" : sep, flags & ~(mask - 1));
669 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
672 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
674 seq_printf(m, "%s%s",
675 first ? "" : sep, obd_connect_names[i]);
680 if (flags2 & ~(mask - 1)) {
681 seq_printf(m, "%sunknown2_%#llx",
682 first ? "" : sep, flags2 & ~(mask - 1));
686 EXPORT_SYMBOL(obd_connect_seq_flags2str);
688 int obd_connect_flags2str(char *page, int count, __u64 flags, __u64 flags2,
694 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
696 ret += snprintf(page + ret, count - ret, "%s%s",
697 ret ? sep : "", obd_connect_names[i]);
700 if (flags & ~(mask - 1))
701 ret += snprintf(page + ret, count - ret,
703 ret ? sep : "", flags & ~(mask - 1));
705 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
708 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
710 ret += snprintf(page + ret, count - ret, "%s%s",
711 ret ? sep : "", obd_connect_names[i]);
714 if (flags2 & ~(mask - 1))
715 ret += snprintf(page + ret, count - ret,
717 ret ? sep : "", flags2 & ~(mask - 1));
721 EXPORT_SYMBOL(obd_connect_flags2str);
724 obd_connect_data_seqprint(struct seq_file *m, struct obd_connect_data *ocd)
728 LASSERT(ocd != NULL);
729 flags = ocd->ocd_connect_flags;
731 seq_printf(m, " connect_data:\n"
734 ocd->ocd_connect_flags,
736 if (flags & OBD_CONNECT_VERSION)
737 seq_printf(m, " target_version: %u.%u.%u.%u\n",
738 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
739 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
740 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
741 OBD_OCD_VERSION_FIX(ocd->ocd_version));
742 if (flags & OBD_CONNECT_MDS)
743 seq_printf(m, " mdt_index: %d\n", ocd->ocd_group);
744 if (flags & OBD_CONNECT_GRANT)
745 seq_printf(m, " initial_grant: %d\n", ocd->ocd_grant);
746 if (flags & OBD_CONNECT_INDEX)
747 seq_printf(m, " target_index: %u\n", ocd->ocd_index);
748 if (flags & OBD_CONNECT_BRW_SIZE)
749 seq_printf(m, " max_brw_size: %d\n", ocd->ocd_brw_size);
750 if (flags & OBD_CONNECT_IBITS)
751 seq_printf(m, " ibits_known: %#llx\n",
752 ocd->ocd_ibits_known);
753 if (flags & OBD_CONNECT_GRANT_PARAM)
754 seq_printf(m, " grant_block_size: %d\n"
755 " grant_inode_size: %d\n"
756 " grant_max_extent_size: %d\n"
757 " grant_extent_tax: %d\n",
758 1 << ocd->ocd_grant_blkbits,
759 1 << ocd->ocd_grant_inobits,
760 ocd->ocd_grant_max_blks << ocd->ocd_grant_blkbits,
761 ocd->ocd_grant_tax_kb << 10);
762 if (flags & OBD_CONNECT_TRANSNO)
763 seq_printf(m, " first_transno: %#llx\n",
765 if (flags & OBD_CONNECT_CKSUM)
766 seq_printf(m, " cksum_types: %#x\n",
767 ocd->ocd_cksum_types);
768 if (flags & OBD_CONNECT_MAX_EASIZE)
769 seq_printf(m, " max_easize: %d\n", ocd->ocd_max_easize);
770 if (flags & OBD_CONNECT_MAXBYTES)
771 seq_printf(m, " max_object_bytes: %llu\n",
773 if (flags & OBD_CONNECT_MULTIMODRPCS)
774 seq_printf(m, " max_mod_rpcs: %hu\n",
775 ocd->ocd_maxmodrpcs);
778 static void lprocfs_import_seq_show_locked(struct seq_file *m,
779 struct obd_device *obd,
780 struct obd_import *imp)
782 char nidstr[LNET_NIDSTR_SIZE];
783 struct lprocfs_counter ret;
784 struct lprocfs_counter_header *header;
785 struct obd_import_conn *conn;
786 struct obd_connect_data *ocd;
791 ocd = &imp->imp_connect_data;
793 seq_printf(m, "import:\n"
797 " connect_flags: [ ",
800 ptlrpc_import_state_name(imp->imp_state));
801 obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
802 imp->imp_connect_data.ocd_connect_flags2,
804 seq_printf(m, " ]\n");
805 obd_connect_data_seqprint(m, ocd);
806 seq_printf(m, " import_flags: [ ");
807 obd_import_flags2str(imp, m);
811 " failover_nids: [ ");
812 spin_lock(&imp->imp_lock);
814 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
815 libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
816 nidstr, sizeof(nidstr));
817 seq_printf(m, "%s%s", j ? ", " : "", nidstr);
820 if (imp->imp_connection)
821 libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
822 nidstr, sizeof(nidstr));
824 strncpy(nidstr, "<none>", sizeof(nidstr));
826 " current_connection: %s\n"
827 " connection_attempts: %u\n"
829 " in-progress_invalidations: %u\n"
834 atomic_read(&imp->imp_inval_count),
835 ktime_get_real_seconds() - imp->imp_last_reply_time);
836 spin_unlock(&imp->imp_lock);
838 if (!obd->obd_svc_stats)
841 header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
842 lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
843 if (ret.lc_count != 0)
844 ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
847 seq_printf(m, " rpcs:\n"
849 " unregistering: %u\n"
851 " avg_waittime: %llu %s\n",
852 atomic_read(&imp->imp_inflight),
853 atomic_read(&imp->imp_unregistering),
854 atomic_read(&imp->imp_timeouts),
855 ret.lc_sum, header->lc_units);
858 for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
859 if (imp->imp_at.iat_portal[j] == 0)
861 k = max_t(unsigned int, k,
862 at_get(&imp->imp_at.iat_service_estimate[j]));
864 seq_printf(m, " service_estimates:\n"
865 " services: %u sec\n"
866 " network: %d sec\n",
868 at_get(&imp->imp_at.iat_net_latency));
870 seq_printf(m, " transactions:\n"
871 " last_replay: %llu\n"
872 " peer_committed: %llu\n"
873 " last_checked: %llu\n",
874 imp->imp_last_replay_transno,
875 imp->imp_peer_committed_transno,
876 imp->imp_last_transno_checked);
879 for (rw = 0; rw <= 1; rw++) {
880 lprocfs_stats_collect(obd->obd_svc_stats,
881 PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
883 if (ret.lc_sum > 0 && ret.lc_count > 0) {
884 ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
885 seq_printf(m, " %s_data_averages:\n"
886 " bytes_per_rpc: %llu\n",
887 rw ? "write" : "read",
891 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
892 header = &obd->obd_svc_stats->ls_cnt_header[j];
893 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
894 if (ret.lc_sum > 0 && ret.lc_count != 0) {
895 ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
896 seq_printf(m, " %s_per_rpc: %llu\n",
897 header->lc_units, ret.lc_sum);
900 seq_printf(m, " MB_per_sec: %u.%.02u\n",
901 k / j, (100 * k / j) % 100);
906 int lprocfs_import_seq_show(struct seq_file *m, void *data)
908 struct obd_device *obd = (struct obd_device *)data;
909 struct obd_import *imp;
912 LASSERT(obd != NULL);
913 with_imp_locked(obd, imp, rv)
914 lprocfs_import_seq_show_locked(m, obd, imp);
917 EXPORT_SYMBOL(lprocfs_import_seq_show);
919 int lprocfs_state_seq_show(struct seq_file *m, void *data)
921 struct obd_device *obd = (struct obd_device *)data;
922 struct obd_import *imp;
926 LASSERT(obd != NULL);
927 with_imp_locked(obd, imp, rc) {
928 seq_printf(m, "current_state: %s\n",
929 ptlrpc_import_state_name(imp->imp_state));
930 seq_printf(m, "state_history:\n");
931 k = imp->imp_state_hist_idx;
932 for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
933 struct import_state_hist *ish =
934 &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
935 if (ish->ish_state == 0)
937 seq_printf(m, " - [ %lld, %s ]\n", (s64)ish->ish_time,
938 ptlrpc_import_state_name(ish->ish_state));
944 EXPORT_SYMBOL(lprocfs_state_seq_show);
946 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
949 for (i = 0; i < AT_BINS; i++)
950 seq_printf(m, "%3u ", at->at_hist[i]);
954 EXPORT_SYMBOL(lprocfs_at_hist_helper);
956 /* See also ptlrpc_lprocfs_timeouts_show_seq */
957 static void lprocfs_timeouts_seq_show_locked(struct seq_file *m,
958 struct obd_device *obd,
959 struct obd_import *imp)
961 timeout_t cur_timeout, worst_timeout;
962 time64_t now, worst_timestamp;
965 LASSERT(obd != NULL);
967 now = ktime_get_real_seconds();
969 /* Some network health info for kicks */
970 seq_printf(m, "%-10s : %lld, %llds ago\n",
971 "last reply", (s64)imp->imp_last_reply_time,
972 (s64)(now - imp->imp_last_reply_time));
974 cur_timeout = at_get(&imp->imp_at.iat_net_latency);
975 worst_timeout = imp->imp_at.iat_net_latency.at_worst_timeout_ever;
976 worst_timestamp = imp->imp_at.iat_net_latency.at_worst_timestamp;
977 seq_printf(m, "%-10s : cur %3u worst %3u (at %lld, %llds ago) ",
978 "network", cur_timeout, worst_timeout, worst_timestamp,
979 now - worst_timestamp);
980 lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
982 for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
983 struct adaptive_timeout *service_est;
985 if (imp->imp_at.iat_portal[i] == 0)
988 service_est = &imp->imp_at.iat_service_estimate[i];
989 cur_timeout = at_get(service_est);
990 worst_timeout = service_est->at_worst_timeout_ever;
991 worst_timestamp = service_est->at_worst_timestamp;
992 seq_printf(m, "portal %-2d : cur %3u worst %3u (at %lld, %llds ago) ",
993 imp->imp_at.iat_portal[i], cur_timeout,
994 worst_timeout, worst_timestamp,
995 now - worst_timestamp);
996 lprocfs_at_hist_helper(m, service_est);
1000 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
1002 struct obd_device *obd = (struct obd_device *)data;
1003 struct obd_import *imp;
1006 with_imp_locked(obd, imp, rc)
1007 lprocfs_timeouts_seq_show_locked(m, obd, imp);
1010 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
1012 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
1014 struct obd_device *obd = data;
1017 struct obd_import *imp;
1020 with_imp_locked(obd, imp, rc) {
1021 flags = imp->imp_connect_data.ocd_connect_flags;
1022 flags2 = imp->imp_connect_data.ocd_connect_flags2;
1023 seq_printf(m, "flags=%#llx\n", flags);
1024 seq_printf(m, "flags2=%#llx\n", flags2);
1025 obd_connect_seq_flags2str(m, flags, flags2, "\n");
1026 seq_printf(m, "\n");
1031 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
1033 static const struct attribute *obd_def_uuid_attrs[] = {
1034 &lustre_attr_uuid.attr,
1038 static const struct attribute *obd_def_attrs[] = {
1039 &lustre_attr_blocksize.attr,
1040 &lustre_attr_kbytestotal.attr,
1041 &lustre_attr_kbytesfree.attr,
1042 &lustre_attr_kbytesavail.attr,
1043 &lustre_attr_filestotal.attr,
1044 &lustre_attr_filesfree.attr,
1045 &lustre_attr_uuid.attr,
1049 static void obd_sysfs_release(struct kobject *kobj)
1051 struct obd_device *obd = container_of(kobj, struct obd_device,
1054 complete(&obd->obd_kobj_unregister);
1057 int lprocfs_obd_setup(struct obd_device *obd, bool uuid_only)
1059 struct ldebugfs_vars *debugfs_vars = NULL;
1062 if (!obd || obd->obd_magic != OBD_DEVICE_MAGIC)
1065 rc = kobject_set_name(&obd->obd_kset.kobj, "%s", obd->obd_name);
1069 obd->obd_ktype.sysfs_ops = &lustre_sysfs_ops;
1070 obd->obd_ktype.release = obd_sysfs_release;
1072 obd->obd_kset.kobj.parent = &obd->obd_type->typ_kobj;
1073 obd->obd_kset.kobj.ktype = &obd->obd_ktype;
1074 init_completion(&obd->obd_kobj_unregister);
1075 rc = kset_register(&obd->obd_kset);
1080 obd->obd_attrs = obd_def_uuid_attrs;
1082 obd->obd_attrs = obd_def_attrs;
1084 rc = sysfs_create_files(&obd->obd_kset.kobj, obd->obd_attrs);
1086 kset_unregister(&obd->obd_kset);
1090 if (!obd->obd_type->typ_procroot)
1091 debugfs_vars = obd->obd_debugfs_vars;
1092 obd->obd_debugfs_entry = debugfs_create_dir(
1093 obd->obd_name, obd->obd_type->typ_debugfs_entry);
1094 ldebugfs_add_vars(obd->obd_debugfs_entry, debugfs_vars, obd);
1096 if (obd->obd_proc_entry || !obd->obd_type->typ_procroot)
1097 GOTO(already_registered, rc);
1099 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1100 obd->obd_type->typ_procroot,
1101 obd->obd_vars, obd);
1102 if (IS_ERR(obd->obd_proc_entry)) {
1103 rc = PTR_ERR(obd->obd_proc_entry);
1104 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1105 obd->obd_proc_entry = NULL;
1107 debugfs_remove_recursive(obd->obd_debugfs_entry);
1108 obd->obd_debugfs_entry = NULL;
1110 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1111 obd->obd_attrs = NULL;
1112 kset_unregister(&obd->obd_kset);
1118 EXPORT_SYMBOL(lprocfs_obd_setup);
1120 int lprocfs_obd_cleanup(struct obd_device *obd)
1125 if (obd->obd_proc_exports_entry) {
1126 /* Should be no exports left */
1127 lprocfs_remove(&obd->obd_proc_exports_entry);
1128 obd->obd_proc_exports_entry = NULL;
1131 if (obd->obd_proc_entry) {
1132 lprocfs_remove(&obd->obd_proc_entry);
1133 obd->obd_proc_entry = NULL;
1136 debugfs_remove_recursive(obd->obd_debugfs_entry);
1137 obd->obd_debugfs_entry = NULL;
1139 /* obd device never allocated a kset */
1140 if (!obd->obd_kset.kobj.state_initialized)
1143 if (obd->obd_attrs) {
1144 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1145 obd->obd_attrs = NULL;
1148 kset_unregister(&obd->obd_kset);
1149 wait_for_completion(&obd->obd_kobj_unregister);
1152 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1154 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1156 struct lprocfs_counter *cntr;
1157 unsigned int percpusize;
1159 unsigned long flags = 0;
1162 LASSERT(stats->ls_percpu[cpuid] == NULL);
1163 LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1165 percpusize = lprocfs_stats_counter_size(stats);
1166 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1167 if (stats->ls_percpu[cpuid]) {
1169 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1170 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1171 spin_lock_irqsave(&stats->ls_lock, flags);
1173 spin_lock(&stats->ls_lock);
1174 if (stats->ls_biggest_alloc_num <= cpuid)
1175 stats->ls_biggest_alloc_num = cpuid + 1;
1176 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1177 spin_unlock_irqrestore(&stats->ls_lock, flags);
1179 spin_unlock(&stats->ls_lock);
1182 /* initialize the ls_percpu[cpuid] non-zero counter */
1183 for (i = 0; i < stats->ls_num; ++i) {
1184 cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1185 cntr->lc_min = LC_MIN_INIT;
1191 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1192 enum lprocfs_stats_flags flags)
1194 struct lprocfs_stats *stats;
1195 unsigned int num_entry;
1196 unsigned int percpusize = 0;
1202 if (lprocfs_no_percpu_stats != 0)
1203 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1205 if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1208 num_entry = num_possible_cpus();
1210 /* alloc percpu pointers for all possible cpu slots */
1211 LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1215 stats->ls_num = num;
1216 stats->ls_flags = flags;
1217 spin_lock_init(&stats->ls_lock);
1219 /* alloc num of counter headers */
1220 CFS_ALLOC_PTR_ARRAY(stats->ls_cnt_header, stats->ls_num);
1221 if (!stats->ls_cnt_header)
1224 if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1225 /* contains only one set counters */
1226 percpusize = lprocfs_stats_counter_size(stats);
1227 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1228 if (!stats->ls_percpu[0])
1230 stats->ls_biggest_alloc_num = 1;
1231 } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1232 /* alloc all percpu data, currently only obd_memory use this */
1233 for (i = 0; i < num_entry; ++i)
1234 if (lprocfs_stats_alloc_one(stats, i) < 0)
1241 lprocfs_free_stats(&stats);
1244 EXPORT_SYMBOL(lprocfs_alloc_stats);
1246 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1248 struct lprocfs_stats *stats = *statsh;
1249 unsigned int num_entry;
1250 unsigned int percpusize;
1253 if (!stats || stats->ls_num == 0)
1257 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1260 num_entry = num_possible_cpus();
1262 percpusize = lprocfs_stats_counter_size(stats);
1263 for (i = 0; i < num_entry; i++)
1264 if (stats->ls_percpu[i])
1265 LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1266 if (stats->ls_cnt_header)
1267 CFS_FREE_PTR_ARRAY(stats->ls_cnt_header, stats->ls_num);
1268 LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1270 EXPORT_SYMBOL(lprocfs_free_stats);
1272 u64 lprocfs_stats_collector(struct lprocfs_stats *stats, int idx,
1273 enum lprocfs_fields_flags field)
1275 unsigned long flags = 0;
1276 unsigned int num_cpu;
1282 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1283 for (i = 0; i < num_cpu; i++) {
1284 struct lprocfs_counter *cntr;
1286 if (!stats->ls_percpu[i])
1289 cntr = lprocfs_stats_counter_get(stats, i, idx);
1290 ret += lprocfs_read_helper(cntr, &stats->ls_cnt_header[idx],
1291 stats->ls_flags, field);
1293 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1296 EXPORT_SYMBOL(lprocfs_stats_collector);
1298 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1300 struct lprocfs_counter *percpu_cntr;
1303 unsigned int num_entry;
1304 unsigned long flags = 0;
1306 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1308 for (i = 0; i < num_entry; i++) {
1309 if (!stats->ls_percpu[i])
1311 for (j = 0; j < stats->ls_num; j++) {
1312 percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1313 percpu_cntr->lc_count = 0;
1314 percpu_cntr->lc_min = LC_MIN_INIT;
1315 percpu_cntr->lc_max = 0;
1316 percpu_cntr->lc_sumsquare = 0;
1317 percpu_cntr->lc_sum = 0;
1318 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1319 percpu_cntr->lc_sum_irq = 0;
1323 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1325 EXPORT_SYMBOL(lprocfs_clear_stats);
1327 static ssize_t lprocfs_stats_seq_write(struct file *file,
1328 const char __user *buf,
1329 size_t len, loff_t *off)
1331 struct seq_file *seq = file->private_data;
1332 struct lprocfs_stats *stats = seq->private;
1334 lprocfs_clear_stats(stats);
1339 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1341 struct lprocfs_stats *stats = p->private;
1343 return (*pos < stats->ls_num) ? pos : NULL;
1346 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1350 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1354 return lprocfs_stats_seq_start(p, pos);
1357 /* seq file export of one lprocfs counter */
1358 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1360 struct lprocfs_stats *stats = p->private;
1361 struct lprocfs_counter_header *hdr;
1362 struct lprocfs_counter ctr;
1363 int idx = *(loff_t *)v;
1366 struct timespec64 now;
1368 ktime_get_real_ts64(&now);
1369 seq_printf(p, "%-25s %llu.%09lu secs.nsecs\n",
1370 "snapshot_time", (s64)now.tv_sec, now.tv_nsec);
1373 hdr = &stats->ls_cnt_header[idx];
1374 lprocfs_stats_collect(stats, idx, &ctr);
1376 if (ctr.lc_count == 0)
1379 seq_printf(p, "%-25s %lld samples [%s]", hdr->lc_name,
1380 ctr.lc_count, hdr->lc_units);
1382 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1383 seq_printf(p, " %lld %lld %lld",
1384 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1385 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1386 seq_printf(p, " %llu", ctr.lc_sumsquare);
1392 static const struct seq_operations lprocfs_stats_seq_sops = {
1393 .start = lprocfs_stats_seq_start,
1394 .stop = lprocfs_stats_seq_stop,
1395 .next = lprocfs_stats_seq_next,
1396 .show = lprocfs_stats_seq_show,
1399 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1401 struct seq_file *seq;
1404 rc = seq_open(file, &lprocfs_stats_seq_sops);
1407 seq = file->private_data;
1408 seq->private = inode->i_private ? inode->i_private : PDE_DATA(inode);
1412 const struct file_operations ldebugfs_stats_seq_fops = {
1413 .owner = THIS_MODULE,
1414 .open = lprocfs_stats_seq_open,
1416 .write = lprocfs_stats_seq_write,
1417 .llseek = seq_lseek,
1418 .release = lprocfs_seq_release,
1420 EXPORT_SYMBOL(ldebugfs_stats_seq_fops);
1422 static const struct proc_ops lprocfs_stats_seq_fops = {
1423 PROC_OWNER(THIS_MODULE)
1424 .proc_open = lprocfs_stats_seq_open,
1425 .proc_read = seq_read,
1426 .proc_write = lprocfs_stats_seq_write,
1427 .proc_lseek = seq_lseek,
1428 .proc_release = lprocfs_seq_release,
1431 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1432 struct lprocfs_stats *stats)
1434 struct proc_dir_entry *entry;
1435 LASSERT(root != NULL);
1437 entry = proc_create_data(name, 0644, root,
1438 &lprocfs_stats_seq_fops, stats);
1443 EXPORT_SYMBOL(lprocfs_register_stats);
1445 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1446 unsigned conf, const char *name, const char *units)
1448 struct lprocfs_counter_header *header;
1449 struct lprocfs_counter *percpu_cntr;
1450 unsigned long flags = 0;
1452 unsigned int num_cpu;
1454 LASSERT(stats != NULL);
1456 header = &stats->ls_cnt_header[index];
1457 LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1458 index, name, units);
1460 header->lc_config = conf;
1461 header->lc_name = name;
1462 header->lc_units = units;
1464 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1465 for (i = 0; i < num_cpu; ++i) {
1466 if (!stats->ls_percpu[i])
1468 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1469 percpu_cntr->lc_count = 0;
1470 percpu_cntr->lc_min = LC_MIN_INIT;
1471 percpu_cntr->lc_max = 0;
1472 percpu_cntr->lc_sumsquare = 0;
1473 percpu_cntr->lc_sum = 0;
1474 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1475 percpu_cntr->lc_sum_irq = 0;
1477 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1479 EXPORT_SYMBOL(lprocfs_counter_init);
1481 static const char * const mps_stats[] = {
1482 [LPROC_MD_CLOSE] = "close",
1483 [LPROC_MD_CREATE] = "create",
1484 [LPROC_MD_ENQUEUE] = "enqueue",
1485 [LPROC_MD_GETATTR] = "getattr",
1486 [LPROC_MD_INTENT_LOCK] = "intent_lock",
1487 [LPROC_MD_LINK] = "link",
1488 [LPROC_MD_RENAME] = "rename",
1489 [LPROC_MD_SETATTR] = "setattr",
1490 [LPROC_MD_FSYNC] = "fsync",
1491 [LPROC_MD_READ_PAGE] = "read_page",
1492 [LPROC_MD_UNLINK] = "unlink",
1493 [LPROC_MD_SETXATTR] = "setxattr",
1494 [LPROC_MD_GETXATTR] = "getxattr",
1495 [LPROC_MD_INTENT_GETATTR_ASYNC] = "intent_getattr_async",
1496 [LPROC_MD_REVALIDATE_LOCK] = "revalidate_lock",
1499 int lprocfs_alloc_md_stats(struct obd_device *obd,
1500 unsigned int num_private_stats)
1502 struct lprocfs_stats *stats;
1503 unsigned int num_stats;
1507 * TODO Ensure that this function is only used where
1508 * appropriate by adding an assertion to the effect that
1509 * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1510 * because mdt_procfs_init() uses this function to allocate
1511 * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1512 * mdt layer does not use the md_ops interface. This is
1513 * confusing and a waste of memory. See LU-2484.
1515 LASSERT(obd->obd_proc_entry != NULL);
1516 LASSERT(obd->obd_md_stats == NULL);
1518 num_stats = ARRAY_SIZE(mps_stats) + num_private_stats;
1519 stats = lprocfs_alloc_stats(num_stats, 0);
1523 for (i = 0; i < ARRAY_SIZE(mps_stats); i++) {
1524 lprocfs_counter_init(stats, i, 0, mps_stats[i], "reqs");
1525 if (!stats->ls_cnt_header[i].lc_name) {
1526 CERROR("Missing md_stat initializer md_op operation at offset %d. Aborting.\n",
1532 rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1534 lprocfs_free_stats(&stats);
1536 obd->obd_md_stats = stats;
1541 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1543 void lprocfs_free_md_stats(struct obd_device *obd)
1545 struct lprocfs_stats *stats = obd->obd_md_stats;
1548 obd->obd_md_stats = NULL;
1549 lprocfs_free_stats(&stats);
1552 EXPORT_SYMBOL(lprocfs_free_md_stats);
1554 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1556 lprocfs_counter_init(ldlm_stats,
1557 LDLM_ENQUEUE - LDLM_FIRST_OPC,
1558 0, "ldlm_enqueue", "reqs");
1559 lprocfs_counter_init(ldlm_stats,
1560 LDLM_CONVERT - LDLM_FIRST_OPC,
1561 0, "ldlm_convert", "reqs");
1562 lprocfs_counter_init(ldlm_stats,
1563 LDLM_CANCEL - LDLM_FIRST_OPC,
1564 0, "ldlm_cancel", "reqs");
1565 lprocfs_counter_init(ldlm_stats,
1566 LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1567 0, "ldlm_bl_callback", "reqs");
1568 lprocfs_counter_init(ldlm_stats,
1569 LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1570 0, "ldlm_cp_callback", "reqs");
1571 lprocfs_counter_init(ldlm_stats,
1572 LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1573 0, "ldlm_gl_callback", "reqs");
1575 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1577 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1578 struct lprocfs_counter_header *header,
1579 enum lprocfs_stats_flags flags,
1580 enum lprocfs_fields_flags field)
1588 case LPROCFS_FIELDS_FLAGS_CONFIG:
1589 ret = header->lc_config;
1591 case LPROCFS_FIELDS_FLAGS_SUM:
1593 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1594 ret += lc->lc_sum_irq;
1596 case LPROCFS_FIELDS_FLAGS_MIN:
1599 case LPROCFS_FIELDS_FLAGS_MAX:
1602 case LPROCFS_FIELDS_FLAGS_AVG:
1603 ret = (lc->lc_max - lc->lc_min) / 2;
1605 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1606 ret = lc->lc_sumsquare;
1608 case LPROCFS_FIELDS_FLAGS_COUNT:
1616 EXPORT_SYMBOL(lprocfs_read_helper);
1619 * string_to_size - convert ASCII string representing a numerical
1620 * value with optional units to 64-bit binary value
1622 * @size: The numerical value extract out of @buffer
1623 * @buffer: passed in string to parse
1624 * @count: length of the @buffer
1626 * This function returns a 64-bit binary value if @buffer contains a valid
1627 * numerical string. The string is parsed to 3 significant figures after
1628 * the decimal point. Support the string containing an optional units at
1629 * the end which can be base 2 or base 10 in value. If no units are given
1630 * the string is assumed to just a numerical value.
1632 * Returns: @count if the string is successfully parsed,
1633 * -errno on invalid input strings. Error values:
1635 * - ``-EINVAL``: @buffer is not a proper numerical string
1636 * - ``-EOVERFLOW``: results does not fit into 64 bits.
1637 * - ``-E2BIG ``: @buffer is too large (not a valid number)
1639 int string_to_size(u64 *size, const char *buffer, size_t count)
1641 /* For string_get_size() it can support values above exabytes,
1642 * (ZiB, YiB) due to breaking the return value into a size and
1643 * bulk size to avoid 64 bit overflow. We don't break the size
1644 * up into block size units so we don't support ZiB or YiB.
1646 static const char *const units_10[] = {
1647 "kB", "MB", "GB", "TB", "PB", "EB",
1649 static const char *const units_2[] = {
1650 "K", "M", "G", "T", "P", "E",
1652 static const char *const *const units_str[] = {
1653 [STRING_UNITS_2] = units_2,
1654 [STRING_UNITS_10] = units_10,
1656 static const unsigned int coeff[] = {
1657 [STRING_UNITS_10] = 1000,
1658 [STRING_UNITS_2] = 1024,
1660 enum string_size_units unit = STRING_UNITS_2;
1661 u64 whole, blk_size = 1;
1662 char kernbuf[22], *end;
1667 if (count >= sizeof(kernbuf)) {
1668 CERROR("count %zd > buffer %zd\n", count, sizeof(kernbuf));
1673 /* The "iB" suffix is optionally allowed for indicating base-2 numbers.
1674 * If suffix is only "B" and not "iB" then we treat it as base-10.
1676 end = strstr(buffer, "B");
1677 if (end && *(end - 1) != 'i')
1678 unit = STRING_UNITS_10;
1680 i = unit == STRING_UNITS_2 ? ARRAY_SIZE(units_2) - 1 :
1681 ARRAY_SIZE(units_10) - 1;
1683 end = strnstr(buffer, units_str[unit][i], count);
1686 blk_size *= coeff[unit];
1692 /* as 'B' is a substring of all units, we need to handle it
1696 /* 'B' is only acceptable letter at this point */
1697 end = strnchr(buffer, count, 'B');
1701 if (count - len > 2 ||
1702 (count - len == 2 && strcmp(end, "B\n") != 0)) {
1703 CDEBUG(D_INFO, "unknown suffix '%s'\n", buffer);
1707 /* kstrtoull will error out if it has non digits */
1711 end = strnchr(buffer, count, '.');
1713 /* need to limit 3 decimal places */
1714 char rem[4] = "000";
1721 /* limit to 3 decimal points */
1722 off = min_t(size_t, 3, strspn(end, "0123456789"));
1723 /* need to limit frac_d to a u32 */
1724 memcpy(rem, end, off);
1725 rc = kstrtoull(rem, 10, &frac);
1729 if (fls64(frac) + fls64(blk_size) - 1 > 64)
1737 snprintf(kernbuf, sizeof(kernbuf), "%.*s", (int)len, buffer);
1738 rc = kstrtoull(kernbuf, 10, &whole);
1742 if (whole != 0 && fls64(whole) + fls64(blk_size) - 1 > 64)
1745 *size += whole * blk_size;
1749 EXPORT_SYMBOL(string_to_size);
1752 * sysfs_memparse - parse a ASCII string to 64-bit binary value,
1753 * with optional units
1755 * @buffer: kernel pointer to input string
1756 * @count: number of bytes in the input @buffer
1757 * @val: (output) binary value returned to caller
1758 * @defunit: default unit suffix to use if none is provided
1760 * Parses a string into a number. The number stored at @buffer is
1761 * potentially suffixed with K, M, G, T, P, E. Besides these other
1762 * valid suffix units are shown in the string_to_size() function.
1763 * If the string lacks a suffix then the defunit is used. The defunit
1764 * should be given as a binary unit (e.g. MiB) as that is the standard
1765 * for tunables in Lustre. If no unit suffix is given (e.g. 'G'), then
1766 * it is assumed to be in binary units.
1768 * Returns: 0 on success or -errno on failure.
1770 int sysfs_memparse(const char *buffer, size_t count, u64 *val,
1771 const char *defunit)
1773 const char *param = buffer;
1777 count = strlen(buffer);
1778 while (count > 0 && isspace(buffer[count - 1]))
1784 /* If there isn't already a unit on this value, append @defunit.
1785 * Units of 'B' don't affect the value, so don't bother adding.
1787 if (!isalpha(buffer[count - 1]) && defunit[0] != 'B') {
1788 if (count + 3 >= sizeof(tmp_buf)) {
1789 CERROR("count %zd > size %zd\n", count, sizeof(param));
1793 scnprintf(tmp_buf, sizeof(tmp_buf), "%.*s%s", (int)count,
1796 count = strlen(param);
1799 rc = string_to_size(val, param, count);
1801 return rc < 0 ? rc : 0;
1803 EXPORT_SYMBOL(sysfs_memparse);
1805 char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1814 if (!memcmp(s1, s2, l2))
1820 EXPORT_SYMBOL(lprocfs_strnstr);
1823 * Find the string \a name in the input \a buffer, and return a pointer to the
1824 * value immediately following \a name, reducing \a count appropriately.
1825 * If \a name is not found the original \a buffer is returned.
1827 char *lprocfs_find_named_value(const char *buffer, const char *name,
1831 size_t buflen = *count;
1833 /* there is no strnstr() in rhel5 and ubuntu kernels */
1834 val = lprocfs_strnstr(buffer, name, buflen);
1836 return (char *)buffer;
1838 val += strlen(name); /* skip prefix */
1839 while (val < buffer + buflen && isspace(*val)) /* skip separator */
1843 while (val < buffer + buflen && isalnum(*val)) {
1848 return val - *count;
1850 EXPORT_SYMBOL(lprocfs_find_named_value);
1852 int lprocfs_seq_create(struct proc_dir_entry *parent,
1855 const struct proc_ops *seq_fops,
1858 struct proc_dir_entry *entry;
1861 /* Disallow secretly (un)writable entries. */
1862 LASSERT(!seq_fops->proc_write == !(mode & 0222));
1864 entry = proc_create_data(name, mode, parent, seq_fops, data);
1871 EXPORT_SYMBOL(lprocfs_seq_create);
1873 int lprocfs_obd_seq_create(struct obd_device *obd,
1876 const struct proc_ops *seq_fops,
1879 return lprocfs_seq_create(obd->obd_proc_entry, name,
1880 mode, seq_fops, data);
1882 EXPORT_SYMBOL(lprocfs_obd_seq_create);
1884 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
1886 if (value >= OBD_HIST_MAX)
1887 value = OBD_HIST_MAX - 1;
1889 spin_lock(&oh->oh_lock);
1890 oh->oh_buckets[value]++;
1891 spin_unlock(&oh->oh_lock);
1893 EXPORT_SYMBOL(lprocfs_oh_tally);
1895 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
1897 unsigned int val = 0;
1899 if (likely(value != 0))
1900 val = min(fls(value - 1), OBD_HIST_MAX);
1902 lprocfs_oh_tally(oh, val);
1904 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
1906 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
1908 unsigned long ret = 0;
1911 for (i = 0; i < OBD_HIST_MAX; i++)
1912 ret += oh->oh_buckets[i];
1915 EXPORT_SYMBOL(lprocfs_oh_sum);
1917 void lprocfs_oh_clear(struct obd_histogram *oh)
1919 spin_lock(&oh->oh_lock);
1920 memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
1921 spin_unlock(&oh->oh_lock);
1923 EXPORT_SYMBOL(lprocfs_oh_clear);
1925 ssize_t lustre_attr_show(struct kobject *kobj,
1926 struct attribute *attr, char *buf)
1928 struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1930 return a->show ? a->show(kobj, attr, buf) : 0;
1932 EXPORT_SYMBOL_GPL(lustre_attr_show);
1934 ssize_t lustre_attr_store(struct kobject *kobj, struct attribute *attr,
1935 const char *buf, size_t len)
1937 struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1939 return a->store ? a->store(kobj, attr, buf, len) : len;
1941 EXPORT_SYMBOL_GPL(lustre_attr_store);
1943 const struct sysfs_ops lustre_sysfs_ops = {
1944 .show = lustre_attr_show,
1945 .store = lustre_attr_store,
1947 EXPORT_SYMBOL_GPL(lustre_sysfs_ops);
1949 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
1951 struct obd_device *obd = data;
1952 struct client_obd *cli = &obd->u.cli;
1954 spin_lock(&cli->cl_loi_list_lock);
1955 seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
1956 spin_unlock(&cli->cl_loi_list_lock);
1959 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
1961 ssize_t lprocfs_obd_max_pages_per_rpc_seq_write(struct file *file,
1962 const char __user *buffer,
1963 size_t count, loff_t *off)
1965 struct seq_file *m = file->private_data;
1966 struct obd_device *obd = m->private;
1967 struct client_obd *cli = &obd->u.cli;
1968 struct obd_import *imp;
1969 struct obd_connect_data *ocd;
1974 if (count > sizeof(kernbuf) - 1)
1977 if (copy_from_user(kernbuf, buffer, count))
1980 kernbuf[count] = '\0';
1982 rc = sysfs_memparse(kernbuf, count, &val, "B");
1986 /* if the max_pages is specified in bytes, convert to pages */
1987 if (val >= ONE_MB_BRW_SIZE)
1990 with_imp_locked(obd, imp, rc) {
1991 ocd = &imp->imp_connect_data;
1992 chunk_mask = ~((1 << (cli->cl_chunkbits - PAGE_SHIFT)) - 1);
1993 /* max_pages_per_rpc must be chunk aligned */
1994 val = (val + ~chunk_mask) & chunk_mask;
1995 if (val == 0 || (ocd->ocd_brw_size != 0 &&
1996 val > ocd->ocd_brw_size >> PAGE_SHIFT)) {
1999 spin_lock(&cli->cl_loi_list_lock);
2000 cli->cl_max_pages_per_rpc = val;
2001 client_adjust_max_dirty(cli);
2002 spin_unlock(&cli->cl_loi_list_lock);
2008 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_write);
2010 ssize_t short_io_bytes_show(struct kobject *kobj, struct attribute *attr,
2013 struct obd_device *obd = container_of(kobj, struct obd_device,
2015 struct client_obd *cli = &obd->u.cli;
2018 spin_lock(&cli->cl_loi_list_lock);
2019 rc = sprintf(buf, "%d\n", cli->cl_max_short_io_bytes);
2020 spin_unlock(&cli->cl_loi_list_lock);
2023 EXPORT_SYMBOL(short_io_bytes_show);
2025 /* Used to catch people who think they're specifying pages. */
2026 #define MIN_SHORT_IO_BYTES 64U
2028 ssize_t short_io_bytes_store(struct kobject *kobj, struct attribute *attr,
2029 const char *buffer, size_t count)
2031 struct obd_device *obd = container_of(kobj, struct obd_device,
2033 struct client_obd *cli = &obd->u.cli;
2037 if (strcmp(buffer, "-1") == 0) {
2038 val = OBD_DEF_SHORT_IO_BYTES;
2040 rc = sysfs_memparse(buffer, count, &val, "B");
2045 if (val && (val < MIN_SHORT_IO_BYTES || val > LNET_MTU))
2046 GOTO(out, rc = -ERANGE);
2050 spin_lock(&cli->cl_loi_list_lock);
2051 cli->cl_max_short_io_bytes = min_t(u64, val, OST_MAX_SHORT_IO_BYTES);
2052 spin_unlock(&cli->cl_loi_list_lock);
2057 EXPORT_SYMBOL(short_io_bytes_store);
2059 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2060 struct root_squash_info *squash, char *name)
2063 char kernbuf[64], *tmp, *errmsg;
2064 unsigned long uid, gid;
2067 if (count >= sizeof(kernbuf)) {
2068 errmsg = "string too long";
2069 GOTO(failed_noprint, rc = -EINVAL);
2071 if (copy_from_user(kernbuf, buffer, count)) {
2072 errmsg = "bad address";
2073 GOTO(failed_noprint, rc = -EFAULT);
2075 kernbuf[count] = '\0';
2077 /* look for uid gid separator */
2078 tmp = strchr(kernbuf, ':');
2080 errmsg = "needs uid:gid format";
2081 GOTO(failed, rc = -EINVAL);
2087 if (kstrtoul(kernbuf, 0, &uid) != 0) {
2089 GOTO(failed, rc = -EINVAL);
2093 if (kstrtoul(tmp, 0, &gid) != 0) {
2095 GOTO(failed, rc = -EINVAL);
2098 squash->rsi_uid = uid;
2099 squash->rsi_gid = gid;
2101 LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2102 name, squash->rsi_uid, squash->rsi_gid);
2110 CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2111 name, kernbuf, errmsg, rc);
2114 CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2118 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2121 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2122 struct root_squash_info *squash, char *name)
2125 char *kernbuf = NULL;
2132 errmsg = "string too long";
2133 GOTO(failed, rc = -EINVAL);
2136 OBD_ALLOC(kernbuf, count + 1);
2138 errmsg = "no memory";
2139 GOTO(failed, rc = -ENOMEM);
2141 if (copy_from_user(kernbuf, buffer, count)) {
2142 errmsg = "bad address";
2143 GOTO(failed, rc = -EFAULT);
2145 kernbuf[count] = '\0';
2147 if (count > 0 && kernbuf[count - 1] == '\n')
2150 if ((len == 4 && strncmp(kernbuf, "NONE", len) == 0) ||
2151 (len == 5 && strncmp(kernbuf, "clear", len) == 0)) {
2152 /* empty string is special case */
2153 spin_lock(&squash->rsi_lock);
2154 if (!list_empty(&squash->rsi_nosquash_nids))
2155 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2156 spin_unlock(&squash->rsi_lock);
2157 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2158 OBD_FREE(kernbuf, count + 1);
2162 if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2163 errmsg = "can't parse";
2164 GOTO(failed, rc = -EINVAL);
2166 LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2168 OBD_FREE(kernbuf, count + 1);
2171 spin_lock(&squash->rsi_lock);
2172 if (!list_empty(&squash->rsi_nosquash_nids))
2173 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2174 list_splice(&tmp, &squash->rsi_nosquash_nids);
2175 spin_unlock(&squash->rsi_lock);
2181 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2182 name, kernbuf, errmsg, rc);
2183 OBD_FREE(kernbuf, count + 1);
2185 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2190 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2192 #endif /* CONFIG_PROC_FS*/