4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/obdclass/lprocfs_status.c
34 * Author: Hariharan Thantry <thantry@users.sourceforge.net>
37 #define DEBUG_SUBSYSTEM S_CLASS
39 #include <obd_class.h>
40 #include <lprocfs_status.h>
44 static int lprocfs_no_percpu_stats = 0;
45 module_param(lprocfs_no_percpu_stats, int, 0644);
46 MODULE_PARM_DESC(lprocfs_no_percpu_stats, "Do not alloc percpu data for lprocfs stats");
48 #define MAX_STRING_SIZE 128
50 int lprocfs_single_release(struct inode *inode, struct file *file)
52 return single_release(inode, file);
54 EXPORT_SYMBOL(lprocfs_single_release);
56 int lprocfs_seq_release(struct inode *inode, struct file *file)
58 return seq_release(inode, file);
60 EXPORT_SYMBOL(lprocfs_seq_release);
62 struct dentry *ldebugfs_add_simple(struct dentry *root,
63 char *name, void *data,
64 const struct file_operations *fops)
69 if (!root || !name || !fops)
70 return ERR_PTR(-EINVAL);
76 entry = debugfs_create_file(name, mode, root, data, fops);
77 if (IS_ERR_OR_NULL(entry)) {
78 CERROR("LprocFS: No memory to create <debugfs> entry %s", name);
79 return entry ?: ERR_PTR(-ENOMEM);
83 EXPORT_SYMBOL(ldebugfs_add_simple);
85 struct proc_dir_entry *
86 lprocfs_add_simple(struct proc_dir_entry *root, char *name,
87 void *data, const struct file_operations *fops)
89 struct proc_dir_entry *proc;
92 if (!root || !name || !fops)
93 return ERR_PTR(-EINVAL);
99 proc = proc_create_data(name, mode, root, fops, data);
101 CERROR("LprocFS: No memory to create /proc entry %s\n",
103 return ERR_PTR(-ENOMEM);
107 EXPORT_SYMBOL(lprocfs_add_simple);
109 struct proc_dir_entry *lprocfs_add_symlink(const char *name,
110 struct proc_dir_entry *parent,
111 const char *format, ...)
113 struct proc_dir_entry *entry;
117 if (!parent || !format)
120 OBD_ALLOC_WAIT(dest, MAX_STRING_SIZE + 1);
124 va_start(ap, format);
125 vsnprintf(dest, MAX_STRING_SIZE, format, ap);
128 entry = proc_symlink(name, parent, dest);
130 CERROR("LprocFS: Could not create symbolic link from "
131 "%s to %s\n", name, dest);
133 OBD_FREE(dest, MAX_STRING_SIZE + 1);
136 EXPORT_SYMBOL(lprocfs_add_symlink);
138 static const struct file_operations lprocfs_generic_fops = { };
140 void ldebugfs_add_vars(struct dentry *parent, struct lprocfs_vars *list,
143 if (IS_ERR_OR_NULL(parent) || IS_ERR_OR_NULL(list))
149 if (list->proc_mode != 0000) {
150 mode = list->proc_mode;
151 } else if (list->fops) {
152 if (list->fops->read)
154 if (list->fops->write)
157 debugfs_create_file(list->name, mode, parent,
159 list->fops ? : &lprocfs_generic_fops);
163 EXPORT_SYMBOL_GPL(ldebugfs_add_vars);
168 * \param root [in] The parent proc entry on which new entry will be added.
169 * \param list [in] Array of proc entries to be added.
170 * \param data [in] The argument to be passed when entries read/write routines
171 * are called through /proc file.
173 * \retval 0 on success
177 lprocfs_add_vars(struct proc_dir_entry *root, struct lprocfs_vars *list,
184 struct proc_dir_entry *proc;
187 if (list->proc_mode != 0000) {
188 mode = list->proc_mode;
189 } else if (list->fops) {
190 if (list->fops->read)
192 if (list->fops->write)
195 proc = proc_create_data(list->name, mode, root,
196 list->fops ?: &lprocfs_generic_fops,
204 EXPORT_SYMBOL(lprocfs_add_vars);
206 void lprocfs_remove(struct proc_dir_entry **rooth)
211 EXPORT_SYMBOL(lprocfs_remove);
213 void lprocfs_remove_proc_entry(const char *name, struct proc_dir_entry *parent)
215 LASSERT(parent != NULL);
216 remove_proc_entry(name, parent);
218 EXPORT_SYMBOL(lprocfs_remove_proc_entry);
220 struct proc_dir_entry *
221 lprocfs_register(const char *name, struct proc_dir_entry *parent,
222 struct lprocfs_vars *list, void *data)
224 struct proc_dir_entry *newchild;
226 newchild = proc_mkdir(name, parent);
228 return ERR_PTR(-ENOMEM);
231 int rc = lprocfs_add_vars(newchild, list, data);
233 lprocfs_remove(&newchild);
239 EXPORT_SYMBOL(lprocfs_register);
241 /* Generic callbacks */
242 int lprocfs_uuid_seq_show(struct seq_file *m, void *data)
244 struct obd_device *obd = data;
246 LASSERT(obd != NULL);
247 seq_printf(m, "%s\n", obd->obd_uuid.uuid);
250 EXPORT_SYMBOL(lprocfs_uuid_seq_show);
252 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
255 struct obd_device *obd = container_of(kobj, struct obd_device,
258 return sprintf(buf, "%s\n", obd->obd_uuid.uuid);
260 LUSTRE_RO_ATTR(uuid);
262 static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
265 struct obd_device *obd = container_of(kobj, struct obd_device,
267 struct obd_statfs osfs;
270 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
271 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
274 return sprintf(buf, "%u\n", osfs.os_bsize);
278 LUSTRE_RO_ATTR(blocksize);
280 static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
283 struct obd_device *obd = container_of(kobj, struct obd_device,
285 struct obd_statfs osfs;
288 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
289 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
292 u32 blk_size = osfs.os_bsize >> 10;
293 u64 result = osfs.os_blocks;
295 result *= rounddown_pow_of_two(blk_size ?: 1);
296 return sprintf(buf, "%llu\n", result);
301 LUSTRE_RO_ATTR(kbytestotal);
303 static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
306 struct obd_device *obd = container_of(kobj, struct obd_device,
308 struct obd_statfs osfs;
311 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
312 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
315 u32 blk_size = osfs.os_bsize >> 10;
316 u64 result = osfs.os_bfree;
318 while (blk_size >>= 1)
321 return sprintf(buf, "%llu\n", result);
326 LUSTRE_RO_ATTR(kbytesfree);
328 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
331 struct obd_device *obd = container_of(kobj, struct obd_device,
333 struct obd_statfs osfs;
336 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
337 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
340 u32 blk_size = osfs.os_bsize >> 10;
341 u64 result = osfs.os_bavail;
343 while (blk_size >>= 1)
346 return sprintf(buf, "%llu\n", result);
351 LUSTRE_RO_ATTR(kbytesavail);
353 static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
356 struct obd_device *obd = container_of(kobj, struct obd_device,
358 struct obd_statfs osfs;
361 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
362 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
365 return sprintf(buf, "%llu\n", osfs.os_files);
369 LUSTRE_RO_ATTR(filestotal);
371 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
374 struct obd_device *obd = container_of(kobj, struct obd_device,
376 struct obd_statfs osfs;
379 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
380 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
383 return sprintf(buf, "%llu\n", osfs.os_ffree);
387 LUSTRE_RO_ATTR(filesfree);
389 ssize_t conn_uuid_show(struct kobject *kobj, struct attribute *attr, char *buf)
391 struct obd_device *obd = container_of(kobj, struct obd_device,
393 struct obd_import *imp;
394 struct ptlrpc_connection *conn;
397 with_imp_locked(obd, imp, count) {
398 conn = imp->imp_connection;
400 count = sprintf(buf, "%s\n", conn->c_remote_uuid.uuid);
402 count = sprintf(buf, "%s\n", "<none>");
407 EXPORT_SYMBOL(conn_uuid_show);
409 int lprocfs_server_uuid_seq_show(struct seq_file *m, void *data)
411 struct obd_device *obd = data;
412 struct obd_import *imp;
413 char *imp_state_name = NULL;
416 LASSERT(obd != NULL);
417 with_imp_locked(obd, imp, rc) {
418 imp_state_name = ptlrpc_import_state_name(imp->imp_state);
419 seq_printf(m, "%s\t%s%s\n", obd2cli_tgt(obd), imp_state_name,
420 imp->imp_deactive ? "\tDEACTIVATED" : "");
425 EXPORT_SYMBOL(lprocfs_server_uuid_seq_show);
427 /** add up per-cpu counters */
430 * Lock statistics structure for access, possibly only on this CPU.
432 * The statistics struct may be allocated with per-CPU structures for
433 * efficient concurrent update (usually only on server-wide stats), or
434 * as a single global struct (e.g. for per-client or per-job statistics),
435 * so the required locking depends on the type of structure allocated.
437 * For per-CPU statistics, pin the thread to the current cpuid so that
438 * will only access the statistics for that CPU. If the stats structure
439 * for the current CPU has not been allocated (or previously freed),
440 * allocate it now. The per-CPU statistics do not need locking since
441 * the thread is pinned to the CPU during update.
443 * For global statistics, lock the stats structure to prevent concurrent update.
445 * \param[in] stats statistics structure to lock
446 * \param[in] opc type of operation:
447 * LPROCFS_GET_SMP_ID: "lock" and return current CPU index
448 * for incrementing statistics for that CPU
449 * LPROCFS_GET_NUM_CPU: "lock" and return number of used
450 * CPU indices to iterate over all indices
451 * \param[out] flags CPU interrupt saved state for IRQ-safe locking
453 * \retval cpuid of current thread or number of allocated structs
454 * \retval negative on error (only for opc LPROCFS_GET_SMP_ID + per-CPU stats)
456 int lprocfs_stats_lock(struct lprocfs_stats *stats,
457 enum lprocfs_stats_lock_ops opc,
458 unsigned long *flags)
460 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
461 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
462 spin_lock_irqsave(&stats->ls_lock, *flags);
464 spin_lock(&stats->ls_lock);
465 return opc == LPROCFS_GET_NUM_CPU ? 1 : 0;
469 case LPROCFS_GET_SMP_ID: {
470 unsigned int cpuid = get_cpu();
472 if (unlikely(!stats->ls_percpu[cpuid])) {
473 int rc = lprocfs_stats_alloc_one(stats, cpuid);
482 case LPROCFS_GET_NUM_CPU:
483 return stats->ls_biggest_alloc_num;
490 * Unlock statistics structure after access.
492 * Unlock the lock acquired via lprocfs_stats_lock() for global statistics,
493 * or unpin this thread from the current cpuid for per-CPU statistics.
495 * This function must be called using the same arguments as used when calling
496 * lprocfs_stats_lock() so that the correct operation can be performed.
498 * \param[in] stats statistics structure to unlock
499 * \param[in] opc type of operation (current cpuid or number of structs)
500 * \param[in] flags CPU interrupt saved state for IRQ-safe locking
502 void lprocfs_stats_unlock(struct lprocfs_stats *stats,
503 enum lprocfs_stats_lock_ops opc,
504 unsigned long *flags)
506 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
507 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
508 spin_unlock_irqrestore(&stats->ls_lock, *flags);
510 spin_unlock(&stats->ls_lock);
511 } else if (opc == LPROCFS_GET_SMP_ID) {
516 /** add up per-cpu counters */
517 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
518 struct lprocfs_counter *cnt)
520 unsigned int num_entry;
521 struct lprocfs_counter *percpu_cntr;
523 unsigned long flags = 0;
525 memset(cnt, 0, sizeof(*cnt));
528 /* set count to 1 to avoid divide-by-zero errs in callers */
533 cnt->lc_min = LC_MIN_INIT;
535 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
537 for (i = 0; i < num_entry; i++) {
538 if (!stats->ls_percpu[i])
540 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
542 cnt->lc_count += percpu_cntr->lc_count;
543 cnt->lc_sum += percpu_cntr->lc_sum;
544 if (percpu_cntr->lc_min < cnt->lc_min)
545 cnt->lc_min = percpu_cntr->lc_min;
546 if (percpu_cntr->lc_max > cnt->lc_max)
547 cnt->lc_max = percpu_cntr->lc_max;
548 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
551 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
554 static void obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
558 if (imp->imp_obd->obd_no_recov) {
559 seq_printf(m, "no_recov");
563 flag2str(imp, invalid);
564 flag2str(imp, deactive);
565 flag2str(imp, replayable);
566 flag2str(imp, delayed_recovery);
567 flag2str(imp, vbr_failed);
568 flag2str(imp, pingable);
569 flag2str(imp, resend_replay);
570 flag2str(imp, no_pinger_recover);
571 flag2str(imp, connect_tried);
574 static const char *obd_connect_names[] = {
593 "remote_client_by_force",
602 "mds_mds_connection",
605 "alt_checksum_algorithm",
641 "file_secctx", /* 0x01 */
642 "lockaheadv2", /* 0x02 */
643 "dir_migrate", /* 0x04 */
644 "sum_statfs", /* 0x08 */
645 "overstriping", /* 0x10 */
648 "lock_convert", /* 0x80 */
649 "archive_id_array", /* 0x100 */
650 "increasing_xid", /* 0x200 */
651 "selinux_policy", /* 0x400 */
654 "crush", /* 0x2000 */
655 "async_discard", /* 0x4000 */
656 "client_encryption", /* 0x8000 */
660 void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, __u64 flags2,
667 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
669 seq_printf(m, "%s%s",
670 first ? "" : sep, obd_connect_names[i]);
675 if (flags & ~(mask - 1)) {
676 seq_printf(m, "%sunknown_%#llx",
677 first ? "" : sep, flags & ~(mask - 1));
681 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
684 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
686 seq_printf(m, "%s%s",
687 first ? "" : sep, obd_connect_names[i]);
692 if (flags2 & ~(mask - 1)) {
693 seq_printf(m, "%sunknown2_%#llx",
694 first ? "" : sep, flags2 & ~(mask - 1));
698 EXPORT_SYMBOL(obd_connect_seq_flags2str);
700 int obd_connect_flags2str(char *page, int count, __u64 flags, __u64 flags2,
706 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
708 ret += snprintf(page + ret, count - ret, "%s%s",
709 ret ? sep : "", obd_connect_names[i]);
712 if (flags & ~(mask - 1))
713 ret += snprintf(page + ret, count - ret,
715 ret ? sep : "", flags & ~(mask - 1));
717 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
720 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
722 ret += snprintf(page + ret, count - ret, "%s%s",
723 ret ? sep : "", obd_connect_names[i]);
726 if (flags2 & ~(mask - 1))
727 ret += snprintf(page + ret, count - ret,
729 ret ? sep : "", flags2 & ~(mask - 1));
733 EXPORT_SYMBOL(obd_connect_flags2str);
736 obd_connect_data_seqprint(struct seq_file *m, struct obd_connect_data *ocd)
740 LASSERT(ocd != NULL);
741 flags = ocd->ocd_connect_flags;
743 seq_printf(m, " connect_data:\n"
746 ocd->ocd_connect_flags,
748 if (flags & OBD_CONNECT_VERSION)
749 seq_printf(m, " target_version: %u.%u.%u.%u\n",
750 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
751 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
752 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
753 OBD_OCD_VERSION_FIX(ocd->ocd_version));
754 if (flags & OBD_CONNECT_MDS)
755 seq_printf(m, " mdt_index: %d\n", ocd->ocd_group);
756 if (flags & OBD_CONNECT_GRANT)
757 seq_printf(m, " initial_grant: %d\n", ocd->ocd_grant);
758 if (flags & OBD_CONNECT_INDEX)
759 seq_printf(m, " target_index: %u\n", ocd->ocd_index);
760 if (flags & OBD_CONNECT_BRW_SIZE)
761 seq_printf(m, " max_brw_size: %d\n", ocd->ocd_brw_size);
762 if (flags & OBD_CONNECT_IBITS)
763 seq_printf(m, " ibits_known: %#llx\n",
764 ocd->ocd_ibits_known);
765 if (flags & OBD_CONNECT_GRANT_PARAM)
766 seq_printf(m, " grant_block_size: %d\n"
767 " grant_inode_size: %d\n"
768 " grant_max_extent_size: %d\n"
769 " grant_extent_tax: %d\n",
770 1 << ocd->ocd_grant_blkbits,
771 1 << ocd->ocd_grant_inobits,
772 ocd->ocd_grant_max_blks << ocd->ocd_grant_blkbits,
773 ocd->ocd_grant_tax_kb << 10);
774 if (flags & OBD_CONNECT_TRANSNO)
775 seq_printf(m, " first_transno: %#llx\n",
777 if (flags & OBD_CONNECT_CKSUM)
778 seq_printf(m, " cksum_types: %#x\n",
779 ocd->ocd_cksum_types);
780 if (flags & OBD_CONNECT_MAX_EASIZE)
781 seq_printf(m, " max_easize: %d\n", ocd->ocd_max_easize);
782 if (flags & OBD_CONNECT_MAXBYTES)
783 seq_printf(m, " max_object_bytes: %llu\n",
785 if (flags & OBD_CONNECT_MULTIMODRPCS)
786 seq_printf(m, " max_mod_rpcs: %hu\n",
787 ocd->ocd_maxmodrpcs);
790 static void lprocfs_import_seq_show_locked(struct seq_file *m,
791 struct obd_device *obd,
792 struct obd_import *imp)
794 char nidstr[LNET_NIDSTR_SIZE];
795 struct lprocfs_counter ret;
796 struct lprocfs_counter_header *header;
797 struct obd_import_conn *conn;
798 struct obd_connect_data *ocd;
803 ocd = &imp->imp_connect_data;
805 seq_printf(m, "import:\n"
809 " connect_flags: [ ",
812 ptlrpc_import_state_name(imp->imp_state));
813 obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
814 imp->imp_connect_data.ocd_connect_flags2,
816 seq_printf(m, " ]\n");
817 obd_connect_data_seqprint(m, ocd);
818 seq_printf(m, " import_flags: [ ");
819 obd_import_flags2str(imp, m);
823 " failover_nids: [ ");
824 spin_lock(&imp->imp_lock);
826 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
827 libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
828 nidstr, sizeof(nidstr));
829 seq_printf(m, "%s%s", j ? ", " : "", nidstr);
832 if (imp->imp_connection)
833 libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
834 nidstr, sizeof(nidstr));
836 strncpy(nidstr, "<none>", sizeof(nidstr));
838 " current_connection: %s\n"
839 " connection_attempts: %u\n"
841 " in-progress_invalidations: %u\n"
846 atomic_read(&imp->imp_inval_count),
847 ktime_get_real_seconds() - imp->imp_last_reply_time);
848 spin_unlock(&imp->imp_lock);
850 if (!obd->obd_svc_stats)
853 header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
854 lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
855 if (ret.lc_count != 0)
856 ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
859 seq_printf(m, " rpcs:\n"
861 " unregistering: %u\n"
863 " avg_waittime: %llu %s\n",
864 atomic_read(&imp->imp_inflight),
865 atomic_read(&imp->imp_unregistering),
866 atomic_read(&imp->imp_timeouts),
867 ret.lc_sum, header->lc_units);
870 for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
871 if (imp->imp_at.iat_portal[j] == 0)
873 k = max_t(unsigned int, k,
874 at_get(&imp->imp_at.iat_service_estimate[j]));
876 seq_printf(m, " service_estimates:\n"
877 " services: %u sec\n"
878 " network: %u sec\n",
880 at_get(&imp->imp_at.iat_net_latency));
882 seq_printf(m, " transactions:\n"
883 " last_replay: %llu\n"
884 " peer_committed: %llu\n"
885 " last_checked: %llu\n",
886 imp->imp_last_replay_transno,
887 imp->imp_peer_committed_transno,
888 imp->imp_last_transno_checked);
891 for (rw = 0; rw <= 1; rw++) {
892 lprocfs_stats_collect(obd->obd_svc_stats,
893 PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
895 if (ret.lc_sum > 0 && ret.lc_count > 0) {
896 ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
897 seq_printf(m, " %s_data_averages:\n"
898 " bytes_per_rpc: %llu\n",
899 rw ? "write" : "read",
903 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
904 header = &obd->obd_svc_stats->ls_cnt_header[j];
905 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
906 if (ret.lc_sum > 0 && ret.lc_count != 0) {
907 ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
908 seq_printf(m, " %s_per_rpc: %llu\n",
909 header->lc_units, ret.lc_sum);
912 seq_printf(m, " MB_per_sec: %u.%.02u\n",
913 k / j, (100 * k / j) % 100);
918 int lprocfs_import_seq_show(struct seq_file *m, void *data)
920 struct obd_device *obd = (struct obd_device *)data;
921 struct obd_import *imp;
924 LASSERT(obd != NULL);
925 with_imp_locked(obd, imp, rv)
926 lprocfs_import_seq_show_locked(m, obd, imp);
929 EXPORT_SYMBOL(lprocfs_import_seq_show);
931 int lprocfs_state_seq_show(struct seq_file *m, void *data)
933 struct obd_device *obd = (struct obd_device *)data;
934 struct obd_import *imp;
938 LASSERT(obd != NULL);
939 with_imp_locked(obd, imp, rc) {
940 seq_printf(m, "current_state: %s\n",
941 ptlrpc_import_state_name(imp->imp_state));
942 seq_printf(m, "state_history:\n");
943 k = imp->imp_state_hist_idx;
944 for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
945 struct import_state_hist *ish =
946 &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
947 if (ish->ish_state == 0)
949 seq_printf(m, " - [ %lld, %s ]\n", (s64)ish->ish_time,
950 ptlrpc_import_state_name(ish->ish_state));
956 EXPORT_SYMBOL(lprocfs_state_seq_show);
958 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
961 for (i = 0; i < AT_BINS; i++)
962 seq_printf(m, "%3u ", at->at_hist[i]);
966 EXPORT_SYMBOL(lprocfs_at_hist_helper);
968 /* See also ptlrpc_lprocfs_timeouts_show_seq */
969 static void lprocfs_timeouts_seq_show_locked(struct seq_file *m,
970 struct obd_device *obd,
971 struct obd_import *imp)
973 unsigned int cur, worst;
974 time64_t now, worstt;
977 LASSERT(obd != NULL);
979 now = ktime_get_real_seconds();
981 /* Some network health info for kicks */
982 seq_printf(m, "%-10s : %lld, %llds ago\n",
983 "last reply", (s64)imp->imp_last_reply_time,
984 (s64)(now - imp->imp_last_reply_time));
986 cur = at_get(&imp->imp_at.iat_net_latency);
987 worst = imp->imp_at.iat_net_latency.at_worst_ever;
988 worstt = imp->imp_at.iat_net_latency.at_worst_time;
989 seq_printf(m, "%-10s : cur %3u worst %3u (at %lld, %llds ago) ",
990 "network", cur, worst, (s64)worstt, (s64)(now - worstt));
991 lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
993 for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
994 if (imp->imp_at.iat_portal[i] == 0)
996 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
997 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
998 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
999 seq_printf(m, "portal %-2d : cur %3u worst %3u (at %lld, %llds ago) ",
1000 imp->imp_at.iat_portal[i], cur, worst, (s64)worstt,
1001 (s64)(now - worstt));
1002 lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
1006 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
1008 struct obd_device *obd = (struct obd_device *)data;
1009 struct obd_import *imp;
1012 with_imp_locked(obd, imp, rc)
1013 lprocfs_timeouts_seq_show_locked(m, obd, imp);
1016 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
1018 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
1020 struct obd_device *obd = data;
1023 struct obd_import *imp;
1026 with_imp_locked(obd, imp, rc) {
1027 flags = imp->imp_connect_data.ocd_connect_flags;
1028 flags2 = imp->imp_connect_data.ocd_connect_flags2;
1029 seq_printf(m, "flags=%#llx\n", flags);
1030 seq_printf(m, "flags2=%#llx\n", flags2);
1031 obd_connect_seq_flags2str(m, flags, flags2, "\n");
1032 seq_printf(m, "\n");
1037 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
1039 static const struct attribute *obd_def_uuid_attrs[] = {
1040 &lustre_attr_uuid.attr,
1044 static const struct attribute *obd_def_attrs[] = {
1045 &lustre_attr_blocksize.attr,
1046 &lustre_attr_kbytestotal.attr,
1047 &lustre_attr_kbytesfree.attr,
1048 &lustre_attr_kbytesavail.attr,
1049 &lustre_attr_filestotal.attr,
1050 &lustre_attr_filesfree.attr,
1051 &lustre_attr_uuid.attr,
1055 static void obd_sysfs_release(struct kobject *kobj)
1057 struct obd_device *obd = container_of(kobj, struct obd_device,
1060 complete(&obd->obd_kobj_unregister);
1063 int lprocfs_obd_setup(struct obd_device *obd, bool uuid_only)
1065 struct lprocfs_vars *debugfs_vars = NULL;
1068 if (!obd || obd->obd_magic != OBD_DEVICE_MAGIC)
1071 rc = kobject_set_name(&obd->obd_kset.kobj, "%s", obd->obd_name);
1075 obd->obd_ktype.sysfs_ops = &lustre_sysfs_ops;
1076 obd->obd_ktype.release = obd_sysfs_release;
1078 obd->obd_kset.kobj.parent = &obd->obd_type->typ_kobj;
1079 obd->obd_kset.kobj.ktype = &obd->obd_ktype;
1080 init_completion(&obd->obd_kobj_unregister);
1081 rc = kset_register(&obd->obd_kset);
1086 obd->obd_attrs = obd_def_uuid_attrs;
1088 obd->obd_attrs = obd_def_attrs;
1090 rc = sysfs_create_files(&obd->obd_kset.kobj, obd->obd_attrs);
1092 kset_unregister(&obd->obd_kset);
1096 if (!obd->obd_type->typ_procroot)
1097 debugfs_vars = obd->obd_vars;
1098 obd->obd_debugfs_entry = debugfs_create_dir(
1099 obd->obd_name, obd->obd_type->typ_debugfs_entry);
1100 ldebugfs_add_vars(obd->obd_debugfs_entry, debugfs_vars, obd);
1102 if (obd->obd_proc_entry || !obd->obd_type->typ_procroot)
1103 GOTO(already_registered, rc);
1105 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1106 obd->obd_type->typ_procroot,
1107 obd->obd_vars, obd);
1108 if (IS_ERR(obd->obd_proc_entry)) {
1109 rc = PTR_ERR(obd->obd_proc_entry);
1110 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1111 obd->obd_proc_entry = NULL;
1113 debugfs_remove_recursive(obd->obd_debugfs_entry);
1114 obd->obd_debugfs_entry = NULL;
1116 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1117 obd->obd_attrs = NULL;
1118 kset_unregister(&obd->obd_kset);
1124 EXPORT_SYMBOL(lprocfs_obd_setup);
1126 int lprocfs_obd_cleanup(struct obd_device *obd)
1131 if (obd->obd_proc_exports_entry) {
1132 /* Should be no exports left */
1133 lprocfs_remove(&obd->obd_proc_exports_entry);
1134 obd->obd_proc_exports_entry = NULL;
1137 if (obd->obd_proc_entry) {
1138 lprocfs_remove(&obd->obd_proc_entry);
1139 obd->obd_proc_entry = NULL;
1142 debugfs_remove_recursive(obd->obd_debugfs_entry);
1143 obd->obd_debugfs_entry = NULL;
1145 /* obd device never allocated a kset */
1146 if (!obd->obd_kset.kobj.state_initialized)
1149 if (obd->obd_attrs) {
1150 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1151 obd->obd_attrs = NULL;
1154 kset_unregister(&obd->obd_kset);
1155 wait_for_completion(&obd->obd_kobj_unregister);
1158 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1160 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1162 struct lprocfs_counter *cntr;
1163 unsigned int percpusize;
1165 unsigned long flags = 0;
1168 LASSERT(stats->ls_percpu[cpuid] == NULL);
1169 LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1171 percpusize = lprocfs_stats_counter_size(stats);
1172 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1173 if (stats->ls_percpu[cpuid]) {
1175 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1176 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1177 spin_lock_irqsave(&stats->ls_lock, flags);
1179 spin_lock(&stats->ls_lock);
1180 if (stats->ls_biggest_alloc_num <= cpuid)
1181 stats->ls_biggest_alloc_num = cpuid + 1;
1182 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1183 spin_unlock_irqrestore(&stats->ls_lock, flags);
1185 spin_unlock(&stats->ls_lock);
1188 /* initialize the ls_percpu[cpuid] non-zero counter */
1189 for (i = 0; i < stats->ls_num; ++i) {
1190 cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1191 cntr->lc_min = LC_MIN_INIT;
1197 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1198 enum lprocfs_stats_flags flags)
1200 struct lprocfs_stats *stats;
1201 unsigned int num_entry;
1202 unsigned int percpusize = 0;
1208 if (lprocfs_no_percpu_stats != 0)
1209 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1211 if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1214 num_entry = num_possible_cpus();
1216 /* alloc percpu pointers for all possible cpu slots */
1217 LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1221 stats->ls_num = num;
1222 stats->ls_flags = flags;
1223 spin_lock_init(&stats->ls_lock);
1225 /* alloc num of counter headers */
1226 CFS_ALLOC_PTR_ARRAY(stats->ls_cnt_header, stats->ls_num);
1227 if (!stats->ls_cnt_header)
1230 if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1231 /* contains only one set counters */
1232 percpusize = lprocfs_stats_counter_size(stats);
1233 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1234 if (!stats->ls_percpu[0])
1236 stats->ls_biggest_alloc_num = 1;
1237 } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1238 /* alloc all percpu data, currently only obd_memory use this */
1239 for (i = 0; i < num_entry; ++i)
1240 if (lprocfs_stats_alloc_one(stats, i) < 0)
1247 lprocfs_free_stats(&stats);
1250 EXPORT_SYMBOL(lprocfs_alloc_stats);
1252 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1254 struct lprocfs_stats *stats = *statsh;
1255 unsigned int num_entry;
1256 unsigned int percpusize;
1259 if (!stats || stats->ls_num == 0)
1263 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1266 num_entry = num_possible_cpus();
1268 percpusize = lprocfs_stats_counter_size(stats);
1269 for (i = 0; i < num_entry; i++)
1270 if (stats->ls_percpu[i])
1271 LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1272 if (stats->ls_cnt_header)
1273 CFS_FREE_PTR_ARRAY(stats->ls_cnt_header, stats->ls_num);
1274 LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1276 EXPORT_SYMBOL(lprocfs_free_stats);
1278 u64 lprocfs_stats_collector(struct lprocfs_stats *stats, int idx,
1279 enum lprocfs_fields_flags field)
1281 unsigned long flags = 0;
1282 unsigned int num_cpu;
1288 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1289 for (i = 0; i < num_cpu; i++) {
1290 struct lprocfs_counter *cntr;
1292 if (!stats->ls_percpu[i])
1295 cntr = lprocfs_stats_counter_get(stats, i, idx);
1296 ret += lprocfs_read_helper(cntr, &stats->ls_cnt_header[idx],
1297 stats->ls_flags, field);
1299 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1302 EXPORT_SYMBOL(lprocfs_stats_collector);
1304 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1306 struct lprocfs_counter *percpu_cntr;
1309 unsigned int num_entry;
1310 unsigned long flags = 0;
1312 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1314 for (i = 0; i < num_entry; i++) {
1315 if (!stats->ls_percpu[i])
1317 for (j = 0; j < stats->ls_num; j++) {
1318 percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1319 percpu_cntr->lc_count = 0;
1320 percpu_cntr->lc_min = LC_MIN_INIT;
1321 percpu_cntr->lc_max = 0;
1322 percpu_cntr->lc_sumsquare = 0;
1323 percpu_cntr->lc_sum = 0;
1324 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1325 percpu_cntr->lc_sum_irq = 0;
1329 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1331 EXPORT_SYMBOL(lprocfs_clear_stats);
1333 static ssize_t lprocfs_stats_seq_write(struct file *file,
1334 const char __user *buf,
1335 size_t len, loff_t *off)
1337 struct seq_file *seq = file->private_data;
1338 struct lprocfs_stats *stats = seq->private;
1340 lprocfs_clear_stats(stats);
1345 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1347 struct lprocfs_stats *stats = p->private;
1349 return (*pos < stats->ls_num) ? pos : NULL;
1352 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1356 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1360 return lprocfs_stats_seq_start(p, pos);
1363 /* seq file export of one lprocfs counter */
1364 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1366 struct lprocfs_stats *stats = p->private;
1367 struct lprocfs_counter_header *hdr;
1368 struct lprocfs_counter ctr;
1369 int idx = *(loff_t *)v;
1372 struct timespec64 now;
1374 ktime_get_real_ts64(&now);
1375 seq_printf(p, "%-25s %llu.%09lu secs.nsecs\n",
1376 "snapshot_time", (s64)now.tv_sec, now.tv_nsec);
1379 hdr = &stats->ls_cnt_header[idx];
1380 lprocfs_stats_collect(stats, idx, &ctr);
1382 if (ctr.lc_count == 0)
1385 seq_printf(p, "%-25s %lld samples [%s]", hdr->lc_name,
1386 ctr.lc_count, hdr->lc_units);
1388 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1389 seq_printf(p, " %lld %lld %lld",
1390 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1391 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1392 seq_printf(p, " %llu", ctr.lc_sumsquare);
1398 static const struct seq_operations lprocfs_stats_seq_sops = {
1399 .start = lprocfs_stats_seq_start,
1400 .stop = lprocfs_stats_seq_stop,
1401 .next = lprocfs_stats_seq_next,
1402 .show = lprocfs_stats_seq_show,
1405 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1407 struct seq_file *seq;
1410 rc = seq_open(file, &lprocfs_stats_seq_sops);
1413 seq = file->private_data;
1414 seq->private = inode->i_private ? inode->i_private : PDE_DATA(inode);
1418 const struct file_operations lprocfs_stats_seq_fops = {
1419 .owner = THIS_MODULE,
1420 .open = lprocfs_stats_seq_open,
1422 .write = lprocfs_stats_seq_write,
1423 .llseek = seq_lseek,
1424 .release = lprocfs_seq_release,
1426 EXPORT_SYMBOL(lprocfs_stats_seq_fops);
1428 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1429 struct lprocfs_stats *stats)
1431 struct proc_dir_entry *entry;
1432 LASSERT(root != NULL);
1434 entry = proc_create_data(name, 0644, root,
1435 &lprocfs_stats_seq_fops, stats);
1440 EXPORT_SYMBOL(lprocfs_register_stats);
1442 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1443 unsigned conf, const char *name, const char *units)
1445 struct lprocfs_counter_header *header;
1446 struct lprocfs_counter *percpu_cntr;
1447 unsigned long flags = 0;
1449 unsigned int num_cpu;
1451 LASSERT(stats != NULL);
1453 header = &stats->ls_cnt_header[index];
1454 LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1455 index, name, units);
1457 header->lc_config = conf;
1458 header->lc_name = name;
1459 header->lc_units = units;
1461 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1462 for (i = 0; i < num_cpu; ++i) {
1463 if (!stats->ls_percpu[i])
1465 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1466 percpu_cntr->lc_count = 0;
1467 percpu_cntr->lc_min = LC_MIN_INIT;
1468 percpu_cntr->lc_max = 0;
1469 percpu_cntr->lc_sumsquare = 0;
1470 percpu_cntr->lc_sum = 0;
1471 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1472 percpu_cntr->lc_sum_irq = 0;
1474 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1476 EXPORT_SYMBOL(lprocfs_counter_init);
1478 static const char * const mps_stats[] = {
1479 [LPROC_MD_CLOSE] = "close",
1480 [LPROC_MD_CREATE] = "create",
1481 [LPROC_MD_ENQUEUE] = "enqueue",
1482 [LPROC_MD_GETATTR] = "getattr",
1483 [LPROC_MD_INTENT_LOCK] = "intent_lock",
1484 [LPROC_MD_LINK] = "link",
1485 [LPROC_MD_RENAME] = "rename",
1486 [LPROC_MD_SETATTR] = "setattr",
1487 [LPROC_MD_FSYNC] = "fsync",
1488 [LPROC_MD_READ_PAGE] = "read_page",
1489 [LPROC_MD_UNLINK] = "unlink",
1490 [LPROC_MD_SETXATTR] = "setxattr",
1491 [LPROC_MD_GETXATTR] = "getxattr",
1492 [LPROC_MD_INTENT_GETATTR_ASYNC] = "intent_getattr_async",
1493 [LPROC_MD_REVALIDATE_LOCK] = "revalidate_lock",
1496 int lprocfs_alloc_md_stats(struct obd_device *obd,
1497 unsigned int num_private_stats)
1499 struct lprocfs_stats *stats;
1500 unsigned int num_stats;
1504 * TODO Ensure that this function is only used where
1505 * appropriate by adding an assertion to the effect that
1506 * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1507 * because mdt_procfs_init() uses this function to allocate
1508 * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1509 * mdt layer does not use the md_ops interface. This is
1510 * confusing and a waste of memory. See LU-2484.
1512 LASSERT(obd->obd_proc_entry != NULL);
1513 LASSERT(obd->obd_md_stats == NULL);
1515 num_stats = ARRAY_SIZE(mps_stats) + num_private_stats;
1516 stats = lprocfs_alloc_stats(num_stats, 0);
1520 for (i = 0; i < ARRAY_SIZE(mps_stats); i++) {
1521 lprocfs_counter_init(stats, i, 0, mps_stats[i], "reqs");
1522 if (!stats->ls_cnt_header[i].lc_name) {
1523 CERROR("Missing md_stat initializer md_op operation at offset %d. Aborting.\n",
1529 rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1531 lprocfs_free_stats(&stats);
1533 obd->obd_md_stats = stats;
1538 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1540 void lprocfs_free_md_stats(struct obd_device *obd)
1542 struct lprocfs_stats *stats = obd->obd_md_stats;
1545 obd->obd_md_stats = NULL;
1546 lprocfs_free_stats(&stats);
1549 EXPORT_SYMBOL(lprocfs_free_md_stats);
1551 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1553 lprocfs_counter_init(ldlm_stats,
1554 LDLM_ENQUEUE - LDLM_FIRST_OPC,
1555 0, "ldlm_enqueue", "reqs");
1556 lprocfs_counter_init(ldlm_stats,
1557 LDLM_CONVERT - LDLM_FIRST_OPC,
1558 0, "ldlm_convert", "reqs");
1559 lprocfs_counter_init(ldlm_stats,
1560 LDLM_CANCEL - LDLM_FIRST_OPC,
1561 0, "ldlm_cancel", "reqs");
1562 lprocfs_counter_init(ldlm_stats,
1563 LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1564 0, "ldlm_bl_callback", "reqs");
1565 lprocfs_counter_init(ldlm_stats,
1566 LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1567 0, "ldlm_cp_callback", "reqs");
1568 lprocfs_counter_init(ldlm_stats,
1569 LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1570 0, "ldlm_gl_callback", "reqs");
1572 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1574 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1575 struct lprocfs_counter_header *header,
1576 enum lprocfs_stats_flags flags,
1577 enum lprocfs_fields_flags field)
1585 case LPROCFS_FIELDS_FLAGS_CONFIG:
1586 ret = header->lc_config;
1588 case LPROCFS_FIELDS_FLAGS_SUM:
1590 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1591 ret += lc->lc_sum_irq;
1593 case LPROCFS_FIELDS_FLAGS_MIN:
1596 case LPROCFS_FIELDS_FLAGS_MAX:
1599 case LPROCFS_FIELDS_FLAGS_AVG:
1600 ret = (lc->lc_max - lc->lc_min) / 2;
1602 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1603 ret = lc->lc_sumsquare;
1605 case LPROCFS_FIELDS_FLAGS_COUNT:
1613 EXPORT_SYMBOL(lprocfs_read_helper);
1616 * string_to_size - convert ASCII string representing a numerical
1617 * value with optional units to 64-bit binary value
1619 * @size: The numerical value extract out of @buffer
1620 * @buffer: passed in string to parse
1621 * @count: length of the @buffer
1623 * This function returns a 64-bit binary value if @buffer contains a valid
1624 * numerical string. The string is parsed to 3 significant figures after
1625 * the decimal point. Support the string containing an optional units at
1626 * the end which can be base 2 or base 10 in value. If no units are given
1627 * the string is assumed to just a numerical value.
1629 * Returns: @count if the string is successfully parsed,
1630 * -errno on invalid input strings. Error values:
1632 * - ``-EINVAL``: @buffer is not a proper numerical string
1633 * - ``-EOVERFLOW``: results does not fit into 64 bits.
1634 * - ``-E2BIG ``: @buffer is too large (not a valid number)
1636 int string_to_size(u64 *size, const char *buffer, size_t count)
1638 /* For string_get_size() it can support values above exabytes,
1639 * (ZiB, YiB) due to breaking the return value into a size and
1640 * bulk size to avoid 64 bit overflow. We don't break the size
1641 * up into block size units so we don't support ZiB or YiB.
1643 static const char *const units_10[] = {
1644 "kB", "MB", "GB", "TB", "PB", "EB",
1646 static const char *const units_2[] = {
1647 "K", "M", "G", "T", "P", "E",
1649 static const char *const *const units_str[] = {
1650 [STRING_UNITS_2] = units_2,
1651 [STRING_UNITS_10] = units_10,
1653 static const unsigned int coeff[] = {
1654 [STRING_UNITS_10] = 1000,
1655 [STRING_UNITS_2] = 1024,
1657 enum string_size_units unit = STRING_UNITS_2;
1658 u64 whole, blk_size = 1;
1659 char kernbuf[22], *end;
1664 if (count >= sizeof(kernbuf)) {
1665 CERROR("count %zd > buffer %zd\n", count, sizeof(kernbuf));
1670 /* The "iB" suffix is optionally allowed for indicating base-2 numbers.
1671 * If suffix is only "B" and not "iB" then we treat it as base-10.
1673 end = strstr(buffer, "B");
1674 if (end && *(end - 1) != 'i')
1675 unit = STRING_UNITS_10;
1677 i = unit == STRING_UNITS_2 ? ARRAY_SIZE(units_2) - 1 :
1678 ARRAY_SIZE(units_10) - 1;
1680 end = strnstr(buffer, units_str[unit][i], count);
1683 blk_size *= coeff[unit];
1689 /* as 'B' is a substring of all units, we need to handle it
1693 /* 'B' is only acceptable letter at this point */
1694 end = strnchr(buffer, count, 'B');
1698 if (count - len > 2 ||
1699 (count - len == 2 && strcmp(end, "B\n") != 0)) {
1700 CDEBUG(D_INFO, "unknown suffix '%s'\n", buffer);
1704 /* kstrtoull will error out if it has non digits */
1708 end = strnchr(buffer, count, '.');
1710 /* need to limit 3 decimal places */
1711 char rem[4] = "000";
1718 /* limit to 3 decimal points */
1719 off = min_t(size_t, 3, strspn(end, "0123456789"));
1720 /* need to limit frac_d to a u32 */
1721 memcpy(rem, end, off);
1722 rc = kstrtoull(rem, 10, &frac);
1726 if (fls64(frac) + fls64(blk_size) - 1 > 64)
1734 snprintf(kernbuf, sizeof(kernbuf), "%.*s", (int)len, buffer);
1735 rc = kstrtoull(kernbuf, 10, &whole);
1739 if (whole != 0 && fls64(whole) + fls64(blk_size) - 1 > 64)
1742 *size += whole * blk_size;
1746 EXPORT_SYMBOL(string_to_size);
1749 * sysfs_memparse - parse a ASCII string to 64-bit binary value,
1750 * with optional units
1752 * @buffer: kernel pointer to input string
1753 * @count: number of bytes in the input @buffer
1754 * @val: (output) binary value returned to caller
1755 * @defunit: default unit suffix to use if none is provided
1757 * Parses a string into a number. The number stored at @buffer is
1758 * potentially suffixed with K, M, G, T, P, E. Besides these other
1759 * valid suffix units are shown in the string_to_size() function.
1760 * If the string lacks a suffix then the defunit is used. The defunit
1761 * should be given as a binary unit (e.g. MiB) as that is the standard
1762 * for tunables in Lustre. If no unit suffix is given (e.g. 'G'), then
1763 * it is assumed to be in binary units.
1765 * Returns: 0 on success or -errno on failure.
1767 int sysfs_memparse(const char *buffer, size_t count, u64 *val,
1768 const char *defunit)
1770 const char *param = buffer;
1774 count = strlen(buffer);
1775 while (count > 0 && isspace(buffer[count - 1]))
1781 /* If there isn't already a unit on this value, append @defunit.
1782 * Units of 'B' don't affect the value, so don't bother adding.
1784 if (!isalpha(buffer[count - 1]) && defunit[0] != 'B') {
1785 if (count + 3 >= sizeof(tmp_buf)) {
1786 CERROR("count %zd > size %zd\n", count, sizeof(param));
1790 scnprintf(tmp_buf, sizeof(tmp_buf), "%.*s%s", (int)count,
1793 count = strlen(param);
1796 rc = string_to_size(val, param, count);
1798 return rc < 0 ? rc : 0;
1800 EXPORT_SYMBOL(sysfs_memparse);
1802 char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1811 if (!memcmp(s1, s2, l2))
1817 EXPORT_SYMBOL(lprocfs_strnstr);
1820 * Find the string \a name in the input \a buffer, and return a pointer to the
1821 * value immediately following \a name, reducing \a count appropriately.
1822 * If \a name is not found the original \a buffer is returned.
1824 char *lprocfs_find_named_value(const char *buffer, const char *name,
1828 size_t buflen = *count;
1830 /* there is no strnstr() in rhel5 and ubuntu kernels */
1831 val = lprocfs_strnstr(buffer, name, buflen);
1833 return (char *)buffer;
1835 val += strlen(name); /* skip prefix */
1836 while (val < buffer + buflen && isspace(*val)) /* skip separator */
1840 while (val < buffer + buflen && isalnum(*val)) {
1845 return val - *count;
1847 EXPORT_SYMBOL(lprocfs_find_named_value);
1849 int lprocfs_seq_create(struct proc_dir_entry *parent,
1852 const struct file_operations *seq_fops,
1855 struct proc_dir_entry *entry;
1858 /* Disallow secretly (un)writable entries. */
1859 LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
1861 entry = proc_create_data(name, mode, parent, seq_fops, data);
1868 EXPORT_SYMBOL(lprocfs_seq_create);
1870 int lprocfs_obd_seq_create(struct obd_device *obd,
1873 const struct file_operations *seq_fops,
1876 return lprocfs_seq_create(obd->obd_proc_entry, name,
1877 mode, seq_fops, data);
1879 EXPORT_SYMBOL(lprocfs_obd_seq_create);
1881 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
1883 if (value >= OBD_HIST_MAX)
1884 value = OBD_HIST_MAX - 1;
1886 spin_lock(&oh->oh_lock);
1887 oh->oh_buckets[value]++;
1888 spin_unlock(&oh->oh_lock);
1890 EXPORT_SYMBOL(lprocfs_oh_tally);
1892 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
1894 unsigned int val = 0;
1896 if (likely(value != 0))
1897 val = min(fls(value - 1), OBD_HIST_MAX);
1899 lprocfs_oh_tally(oh, val);
1901 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
1903 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
1905 unsigned long ret = 0;
1908 for (i = 0; i < OBD_HIST_MAX; i++)
1909 ret += oh->oh_buckets[i];
1912 EXPORT_SYMBOL(lprocfs_oh_sum);
1914 void lprocfs_oh_clear(struct obd_histogram *oh)
1916 spin_lock(&oh->oh_lock);
1917 memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
1918 spin_unlock(&oh->oh_lock);
1920 EXPORT_SYMBOL(lprocfs_oh_clear);
1922 ssize_t lustre_attr_show(struct kobject *kobj,
1923 struct attribute *attr, char *buf)
1925 struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1927 return a->show ? a->show(kobj, attr, buf) : 0;
1929 EXPORT_SYMBOL_GPL(lustre_attr_show);
1931 ssize_t lustre_attr_store(struct kobject *kobj, struct attribute *attr,
1932 const char *buf, size_t len)
1934 struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1936 return a->store ? a->store(kobj, attr, buf, len) : len;
1938 EXPORT_SYMBOL_GPL(lustre_attr_store);
1940 const struct sysfs_ops lustre_sysfs_ops = {
1941 .show = lustre_attr_show,
1942 .store = lustre_attr_store,
1944 EXPORT_SYMBOL_GPL(lustre_sysfs_ops);
1946 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
1948 struct obd_device *obd = data;
1949 struct client_obd *cli = &obd->u.cli;
1951 spin_lock(&cli->cl_loi_list_lock);
1952 seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
1953 spin_unlock(&cli->cl_loi_list_lock);
1956 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
1958 ssize_t lprocfs_obd_max_pages_per_rpc_seq_write(struct file *file,
1959 const char __user *buffer,
1960 size_t count, loff_t *off)
1962 struct seq_file *m = file->private_data;
1963 struct obd_device *obd = m->private;
1964 struct client_obd *cli = &obd->u.cli;
1965 struct obd_import *imp;
1966 struct obd_connect_data *ocd;
1971 if (count > sizeof(kernbuf) - 1)
1974 if (copy_from_user(kernbuf, buffer, count))
1977 kernbuf[count] = '\0';
1979 rc = sysfs_memparse(kernbuf, count, &val, "B");
1983 /* if the max_pages is specified in bytes, convert to pages */
1984 if (val >= ONE_MB_BRW_SIZE)
1987 with_imp_locked(obd, imp, rc) {
1988 ocd = &imp->imp_connect_data;
1989 chunk_mask = ~((1 << (cli->cl_chunkbits - PAGE_SHIFT)) - 1);
1990 /* max_pages_per_rpc must be chunk aligned */
1991 val = (val + ~chunk_mask) & chunk_mask;
1992 if (val == 0 || (ocd->ocd_brw_size != 0 &&
1993 val > ocd->ocd_brw_size >> PAGE_SHIFT)) {
1996 spin_lock(&cli->cl_loi_list_lock);
1997 cli->cl_max_pages_per_rpc = val;
1998 client_adjust_max_dirty(cli);
1999 spin_unlock(&cli->cl_loi_list_lock);
2005 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_write);
2007 ssize_t short_io_bytes_show(struct kobject *kobj, struct attribute *attr,
2010 struct obd_device *obd = container_of(kobj, struct obd_device,
2012 struct client_obd *cli = &obd->u.cli;
2015 spin_lock(&cli->cl_loi_list_lock);
2016 rc = sprintf(buf, "%d\n", cli->cl_max_short_io_bytes);
2017 spin_unlock(&cli->cl_loi_list_lock);
2020 EXPORT_SYMBOL(short_io_bytes_show);
2022 /* Used to catch people who think they're specifying pages. */
2023 #define MIN_SHORT_IO_BYTES 64U
2025 ssize_t short_io_bytes_store(struct kobject *kobj, struct attribute *attr,
2026 const char *buffer, size_t count)
2028 struct obd_device *obd = container_of(kobj, struct obd_device,
2030 struct client_obd *cli = &obd->u.cli;
2034 if (strcmp(buffer, "-1") == 0) {
2035 val = OBD_DEF_SHORT_IO_BYTES;
2037 rc = sysfs_memparse(buffer, count, &val, "B");
2042 if (val && (val < MIN_SHORT_IO_BYTES || val > LNET_MTU))
2043 GOTO(out, rc = -ERANGE);
2047 spin_lock(&cli->cl_loi_list_lock);
2048 cli->cl_max_short_io_bytes = min_t(u64, val, OST_MAX_SHORT_IO_BYTES);
2049 spin_unlock(&cli->cl_loi_list_lock);
2054 EXPORT_SYMBOL(short_io_bytes_store);
2056 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2057 struct root_squash_info *squash, char *name)
2060 char kernbuf[64], *tmp, *errmsg;
2061 unsigned long uid, gid;
2064 if (count >= sizeof(kernbuf)) {
2065 errmsg = "string too long";
2066 GOTO(failed_noprint, rc = -EINVAL);
2068 if (copy_from_user(kernbuf, buffer, count)) {
2069 errmsg = "bad address";
2070 GOTO(failed_noprint, rc = -EFAULT);
2072 kernbuf[count] = '\0';
2074 /* look for uid gid separator */
2075 tmp = strchr(kernbuf, ':');
2077 errmsg = "needs uid:gid format";
2078 GOTO(failed, rc = -EINVAL);
2084 if (kstrtoul(kernbuf, 0, &uid) != 0) {
2086 GOTO(failed, rc = -EINVAL);
2090 if (kstrtoul(tmp, 0, &gid) != 0) {
2092 GOTO(failed, rc = -EINVAL);
2095 squash->rsi_uid = uid;
2096 squash->rsi_gid = gid;
2098 LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2099 name, squash->rsi_uid, squash->rsi_gid);
2107 CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2108 name, kernbuf, errmsg, rc);
2111 CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2115 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2118 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2119 struct root_squash_info *squash, char *name)
2122 char *kernbuf = NULL;
2129 errmsg = "string too long";
2130 GOTO(failed, rc = -EINVAL);
2133 OBD_ALLOC(kernbuf, count + 1);
2135 errmsg = "no memory";
2136 GOTO(failed, rc = -ENOMEM);
2138 if (copy_from_user(kernbuf, buffer, count)) {
2139 errmsg = "bad address";
2140 GOTO(failed, rc = -EFAULT);
2142 kernbuf[count] = '\0';
2144 if (count > 0 && kernbuf[count - 1] == '\n')
2147 if ((len == 4 && strncmp(kernbuf, "NONE", len) == 0) ||
2148 (len == 5 && strncmp(kernbuf, "clear", len) == 0)) {
2149 /* empty string is special case */
2150 spin_lock(&squash->rsi_lock);
2151 if (!list_empty(&squash->rsi_nosquash_nids))
2152 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2153 spin_unlock(&squash->rsi_lock);
2154 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2155 OBD_FREE(kernbuf, count + 1);
2159 if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2160 errmsg = "can't parse";
2161 GOTO(failed, rc = -EINVAL);
2163 LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2165 OBD_FREE(kernbuf, count + 1);
2168 spin_lock(&squash->rsi_lock);
2169 if (!list_empty(&squash->rsi_nosquash_nids))
2170 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2171 list_splice(&tmp, &squash->rsi_nosquash_nids);
2172 spin_unlock(&squash->rsi_lock);
2178 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2179 name, kernbuf, errmsg, rc);
2180 OBD_FREE(kernbuf, count + 1);
2182 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2187 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2189 #endif /* CONFIG_PROC_FS*/