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 proc_dir_entry *
63 lprocfs_add_simple(struct proc_dir_entry *root, char *name,
64 void *data, const struct file_operations *fops)
66 struct proc_dir_entry *proc;
69 if (!root || !name || !fops)
70 return ERR_PTR(-EINVAL);
76 proc = proc_create_data(name, mode, root, fops, data);
78 CERROR("LprocFS: No memory to create /proc entry %s\n",
80 return ERR_PTR(-ENOMEM);
84 EXPORT_SYMBOL(lprocfs_add_simple);
86 struct proc_dir_entry *lprocfs_add_symlink(const char *name,
87 struct proc_dir_entry *parent,
88 const char *format, ...)
90 struct proc_dir_entry *entry;
94 if (!parent || !format)
97 OBD_ALLOC_WAIT(dest, MAX_STRING_SIZE + 1);
101 va_start(ap, format);
102 vsnprintf(dest, MAX_STRING_SIZE, format, ap);
105 entry = proc_symlink(name, parent, dest);
107 CERROR("LprocFS: Could not create symbolic link from "
108 "%s to %s\n", name, dest);
110 OBD_FREE(dest, MAX_STRING_SIZE + 1);
113 EXPORT_SYMBOL(lprocfs_add_symlink);
115 static const struct file_operations lprocfs_generic_fops = { };
117 void ldebugfs_add_vars(struct dentry *parent, struct lprocfs_vars *list,
120 if (IS_ERR_OR_NULL(parent) || IS_ERR_OR_NULL(list))
126 if (list->proc_mode != 0000) {
127 mode = list->proc_mode;
128 } else if (list->fops) {
129 if (list->fops->read)
131 if (list->fops->write)
134 debugfs_create_file(list->name, mode, parent,
136 list->fops ? : &lprocfs_generic_fops);
140 EXPORT_SYMBOL_GPL(ldebugfs_add_vars);
145 * \param root [in] The parent proc entry on which new entry will be added.
146 * \param list [in] Array of proc entries to be added.
147 * \param data [in] The argument to be passed when entries read/write routines
148 * are called through /proc file.
150 * \retval 0 on success
154 lprocfs_add_vars(struct proc_dir_entry *root, struct lprocfs_vars *list,
161 struct proc_dir_entry *proc;
164 if (list->proc_mode != 0000) {
165 mode = list->proc_mode;
166 } else if (list->fops) {
167 if (list->fops->read)
169 if (list->fops->write)
172 proc = proc_create_data(list->name, mode, root,
173 list->fops ?: &lprocfs_generic_fops,
181 EXPORT_SYMBOL(lprocfs_add_vars);
183 void lprocfs_remove(struct proc_dir_entry **rooth)
188 EXPORT_SYMBOL(lprocfs_remove);
190 void lprocfs_remove_proc_entry(const char *name, struct proc_dir_entry *parent)
192 LASSERT(parent != NULL);
193 remove_proc_entry(name, parent);
195 EXPORT_SYMBOL(lprocfs_remove_proc_entry);
197 struct proc_dir_entry *
198 lprocfs_register(const char *name, struct proc_dir_entry *parent,
199 struct lprocfs_vars *list, void *data)
201 struct proc_dir_entry *newchild;
203 newchild = proc_mkdir(name, parent);
205 return ERR_PTR(-ENOMEM);
208 int rc = lprocfs_add_vars(newchild, list, data);
210 lprocfs_remove(&newchild);
216 EXPORT_SYMBOL(lprocfs_register);
218 /* Generic callbacks */
219 int lprocfs_uuid_seq_show(struct seq_file *m, void *data)
221 struct obd_device *obd = data;
223 LASSERT(obd != NULL);
224 seq_printf(m, "%s\n", obd->obd_uuid.uuid);
227 EXPORT_SYMBOL(lprocfs_uuid_seq_show);
229 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
232 struct obd_device *obd = container_of(kobj, struct obd_device,
235 return sprintf(buf, "%s\n", obd->obd_uuid.uuid);
237 LUSTRE_RO_ATTR(uuid);
239 static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
242 struct obd_device *obd = container_of(kobj, struct obd_device,
244 struct obd_statfs osfs;
247 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
248 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
251 return sprintf(buf, "%u\n", osfs.os_bsize);
255 LUSTRE_RO_ATTR(blocksize);
257 static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
260 struct obd_device *obd = container_of(kobj, struct obd_device,
262 struct obd_statfs osfs;
265 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
266 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
269 u32 blk_size = osfs.os_bsize >> 10;
270 u64 result = osfs.os_blocks;
272 result *= rounddown_pow_of_two(blk_size ?: 1);
273 return sprintf(buf, "%llu\n", result);
278 LUSTRE_RO_ATTR(kbytestotal);
280 static ssize_t kbytesfree_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_bfree;
295 while (blk_size >>= 1)
298 return sprintf(buf, "%llu\n", result);
303 LUSTRE_RO_ATTR(kbytesfree);
305 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
308 struct obd_device *obd = container_of(kobj, struct obd_device,
310 struct obd_statfs osfs;
313 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
314 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
317 u32 blk_size = osfs.os_bsize >> 10;
318 u64 result = osfs.os_bavail;
320 while (blk_size >>= 1)
323 return sprintf(buf, "%llu\n", result);
328 LUSTRE_RO_ATTR(kbytesavail);
330 static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
333 struct obd_device *obd = container_of(kobj, struct obd_device,
335 struct obd_statfs osfs;
338 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
339 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
342 return sprintf(buf, "%llu\n", osfs.os_files);
346 LUSTRE_RO_ATTR(filestotal);
348 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
351 struct obd_device *obd = container_of(kobj, struct obd_device,
353 struct obd_statfs osfs;
356 rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
357 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
360 return sprintf(buf, "%llu\n", osfs.os_ffree);
364 LUSTRE_RO_ATTR(filesfree);
366 ssize_t conn_uuid_show(struct kobject *kobj, struct attribute *attr, char *buf)
368 struct obd_device *obd = container_of(kobj, struct obd_device,
370 struct obd_import *imp;
371 struct ptlrpc_connection *conn;
374 with_imp_locked(obd, imp, count) {
375 conn = imp->imp_connection;
377 count = sprintf(buf, "%s\n", conn->c_remote_uuid.uuid);
379 count = sprintf(buf, "%s\n", "<none>");
384 EXPORT_SYMBOL(conn_uuid_show);
386 int lprocfs_server_uuid_seq_show(struct seq_file *m, void *data)
388 struct obd_device *obd = data;
389 struct obd_import *imp;
390 char *imp_state_name = NULL;
393 LASSERT(obd != NULL);
394 with_imp_locked(obd, imp, rc) {
395 imp_state_name = ptlrpc_import_state_name(imp->imp_state);
396 seq_printf(m, "%s\t%s%s\n", obd2cli_tgt(obd), imp_state_name,
397 imp->imp_deactive ? "\tDEACTIVATED" : "");
402 EXPORT_SYMBOL(lprocfs_server_uuid_seq_show);
404 /** add up per-cpu counters */
407 * Lock statistics structure for access, possibly only on this CPU.
409 * The statistics struct may be allocated with per-CPU structures for
410 * efficient concurrent update (usually only on server-wide stats), or
411 * as a single global struct (e.g. for per-client or per-job statistics),
412 * so the required locking depends on the type of structure allocated.
414 * For per-CPU statistics, pin the thread to the current cpuid so that
415 * will only access the statistics for that CPU. If the stats structure
416 * for the current CPU has not been allocated (or previously freed),
417 * allocate it now. The per-CPU statistics do not need locking since
418 * the thread is pinned to the CPU during update.
420 * For global statistics, lock the stats structure to prevent concurrent update.
422 * \param[in] stats statistics structure to lock
423 * \param[in] opc type of operation:
424 * LPROCFS_GET_SMP_ID: "lock" and return current CPU index
425 * for incrementing statistics for that CPU
426 * LPROCFS_GET_NUM_CPU: "lock" and return number of used
427 * CPU indices to iterate over all indices
428 * \param[out] flags CPU interrupt saved state for IRQ-safe locking
430 * \retval cpuid of current thread or number of allocated structs
431 * \retval negative on error (only for opc LPROCFS_GET_SMP_ID + per-CPU stats)
433 int lprocfs_stats_lock(struct lprocfs_stats *stats,
434 enum lprocfs_stats_lock_ops opc,
435 unsigned long *flags)
437 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
438 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
439 spin_lock_irqsave(&stats->ls_lock, *flags);
441 spin_lock(&stats->ls_lock);
442 return opc == LPROCFS_GET_NUM_CPU ? 1 : 0;
446 case LPROCFS_GET_SMP_ID: {
447 unsigned int cpuid = get_cpu();
449 if (unlikely(!stats->ls_percpu[cpuid])) {
450 int rc = lprocfs_stats_alloc_one(stats, cpuid);
459 case LPROCFS_GET_NUM_CPU:
460 return stats->ls_biggest_alloc_num;
467 * Unlock statistics structure after access.
469 * Unlock the lock acquired via lprocfs_stats_lock() for global statistics,
470 * or unpin this thread from the current cpuid for per-CPU statistics.
472 * This function must be called using the same arguments as used when calling
473 * lprocfs_stats_lock() so that the correct operation can be performed.
475 * \param[in] stats statistics structure to unlock
476 * \param[in] opc type of operation (current cpuid or number of structs)
477 * \param[in] flags CPU interrupt saved state for IRQ-safe locking
479 void lprocfs_stats_unlock(struct lprocfs_stats *stats,
480 enum lprocfs_stats_lock_ops opc,
481 unsigned long *flags)
483 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
484 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
485 spin_unlock_irqrestore(&stats->ls_lock, *flags);
487 spin_unlock(&stats->ls_lock);
488 } else if (opc == LPROCFS_GET_SMP_ID) {
493 /** add up per-cpu counters */
494 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
495 struct lprocfs_counter *cnt)
497 unsigned int num_entry;
498 struct lprocfs_counter *percpu_cntr;
500 unsigned long flags = 0;
502 memset(cnt, 0, sizeof(*cnt));
505 /* set count to 1 to avoid divide-by-zero errs in callers */
510 cnt->lc_min = LC_MIN_INIT;
512 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
514 for (i = 0; i < num_entry; i++) {
515 if (!stats->ls_percpu[i])
517 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
519 cnt->lc_count += percpu_cntr->lc_count;
520 cnt->lc_sum += percpu_cntr->lc_sum;
521 if (percpu_cntr->lc_min < cnt->lc_min)
522 cnt->lc_min = percpu_cntr->lc_min;
523 if (percpu_cntr->lc_max > cnt->lc_max)
524 cnt->lc_max = percpu_cntr->lc_max;
525 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
528 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
531 static void obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
535 if (imp->imp_obd->obd_no_recov) {
536 seq_printf(m, "no_recov");
540 flag2str(imp, invalid);
541 flag2str(imp, deactive);
542 flag2str(imp, replayable);
543 flag2str(imp, delayed_recovery);
544 flag2str(imp, vbr_failed);
545 flag2str(imp, pingable);
546 flag2str(imp, resend_replay);
547 flag2str(imp, no_pinger_recover);
548 flag2str(imp, connect_tried);
551 static const char *obd_connect_names[] = {
570 "remote_client_by_force",
579 "mds_mds_connection",
582 "alt_checksum_algorithm",
618 "file_secctx", /* 0x01 */
619 "lockaheadv2", /* 0x02 */
620 "dir_migrate", /* 0x04 */
621 "sum_statfs", /* 0x08 */
622 "overstriping", /* 0x10 */
625 "lock_convert", /* 0x80 */
626 "archive_id_array", /* 0x100 */
627 "increasing_xid", /* 0x200 */
628 "selinux_policy", /* 0x400 */
631 "crush", /* 0x2000 */
632 "async_discard", /* 0x4000 */
633 "client_encryption", /* 0x8000 */
637 void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, __u64 flags2,
644 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
646 seq_printf(m, "%s%s",
647 first ? "" : sep, obd_connect_names[i]);
652 if (flags & ~(mask - 1)) {
653 seq_printf(m, "%sunknown_%#llx",
654 first ? "" : sep, flags & ~(mask - 1));
658 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
661 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
663 seq_printf(m, "%s%s",
664 first ? "" : sep, obd_connect_names[i]);
669 if (flags2 & ~(mask - 1)) {
670 seq_printf(m, "%sunknown2_%#llx",
671 first ? "" : sep, flags2 & ~(mask - 1));
675 EXPORT_SYMBOL(obd_connect_seq_flags2str);
677 int obd_connect_flags2str(char *page, int count, __u64 flags, __u64 flags2,
683 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
685 ret += snprintf(page + ret, count - ret, "%s%s",
686 ret ? sep : "", obd_connect_names[i]);
689 if (flags & ~(mask - 1))
690 ret += snprintf(page + ret, count - ret,
692 ret ? sep : "", flags & ~(mask - 1));
694 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
697 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
699 ret += snprintf(page + ret, count - ret, "%s%s",
700 ret ? sep : "", obd_connect_names[i]);
703 if (flags2 & ~(mask - 1))
704 ret += snprintf(page + ret, count - ret,
706 ret ? sep : "", flags2 & ~(mask - 1));
710 EXPORT_SYMBOL(obd_connect_flags2str);
713 obd_connect_data_seqprint(struct seq_file *m, struct obd_connect_data *ocd)
717 LASSERT(ocd != NULL);
718 flags = ocd->ocd_connect_flags;
720 seq_printf(m, " connect_data:\n"
723 ocd->ocd_connect_flags,
725 if (flags & OBD_CONNECT_VERSION)
726 seq_printf(m, " target_version: %u.%u.%u.%u\n",
727 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
728 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
729 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
730 OBD_OCD_VERSION_FIX(ocd->ocd_version));
731 if (flags & OBD_CONNECT_MDS)
732 seq_printf(m, " mdt_index: %d\n", ocd->ocd_group);
733 if (flags & OBD_CONNECT_GRANT)
734 seq_printf(m, " initial_grant: %d\n", ocd->ocd_grant);
735 if (flags & OBD_CONNECT_INDEX)
736 seq_printf(m, " target_index: %u\n", ocd->ocd_index);
737 if (flags & OBD_CONNECT_BRW_SIZE)
738 seq_printf(m, " max_brw_size: %d\n", ocd->ocd_brw_size);
739 if (flags & OBD_CONNECT_IBITS)
740 seq_printf(m, " ibits_known: %#llx\n",
741 ocd->ocd_ibits_known);
742 if (flags & OBD_CONNECT_GRANT_PARAM)
743 seq_printf(m, " grant_block_size: %d\n"
744 " grant_inode_size: %d\n"
745 " grant_max_extent_size: %d\n"
746 " grant_extent_tax: %d\n",
747 1 << ocd->ocd_grant_blkbits,
748 1 << ocd->ocd_grant_inobits,
749 ocd->ocd_grant_max_blks << ocd->ocd_grant_blkbits,
750 ocd->ocd_grant_tax_kb << 10);
751 if (flags & OBD_CONNECT_TRANSNO)
752 seq_printf(m, " first_transno: %#llx\n",
754 if (flags & OBD_CONNECT_CKSUM)
755 seq_printf(m, " cksum_types: %#x\n",
756 ocd->ocd_cksum_types);
757 if (flags & OBD_CONNECT_MAX_EASIZE)
758 seq_printf(m, " max_easize: %d\n", ocd->ocd_max_easize);
759 if (flags & OBD_CONNECT_MAXBYTES)
760 seq_printf(m, " max_object_bytes: %llu\n",
762 if (flags & OBD_CONNECT_MULTIMODRPCS)
763 seq_printf(m, " max_mod_rpcs: %hu\n",
764 ocd->ocd_maxmodrpcs);
767 static void lprocfs_import_seq_show_locked(struct seq_file *m,
768 struct obd_device *obd,
769 struct obd_import *imp)
771 char nidstr[LNET_NIDSTR_SIZE];
772 struct lprocfs_counter ret;
773 struct lprocfs_counter_header *header;
774 struct obd_import_conn *conn;
775 struct obd_connect_data *ocd;
780 ocd = &imp->imp_connect_data;
782 seq_printf(m, "import:\n"
786 " connect_flags: [ ",
789 ptlrpc_import_state_name(imp->imp_state));
790 obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
791 imp->imp_connect_data.ocd_connect_flags2,
793 seq_printf(m, " ]\n");
794 obd_connect_data_seqprint(m, ocd);
795 seq_printf(m, " import_flags: [ ");
796 obd_import_flags2str(imp, m);
800 " failover_nids: [ ");
801 spin_lock(&imp->imp_lock);
803 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
804 libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
805 nidstr, sizeof(nidstr));
806 seq_printf(m, "%s%s", j ? ", " : "", nidstr);
809 if (imp->imp_connection)
810 libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
811 nidstr, sizeof(nidstr));
813 strncpy(nidstr, "<none>", sizeof(nidstr));
815 " current_connection: %s\n"
816 " connection_attempts: %u\n"
818 " in-progress_invalidations: %u\n"
823 atomic_read(&imp->imp_inval_count),
824 ktime_get_real_seconds() - imp->imp_last_reply_time);
825 spin_unlock(&imp->imp_lock);
827 if (!obd->obd_svc_stats)
830 header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
831 lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
832 if (ret.lc_count != 0)
833 ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
836 seq_printf(m, " rpcs:\n"
838 " unregistering: %u\n"
840 " avg_waittime: %llu %s\n",
841 atomic_read(&imp->imp_inflight),
842 atomic_read(&imp->imp_unregistering),
843 atomic_read(&imp->imp_timeouts),
844 ret.lc_sum, header->lc_units);
847 for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
848 if (imp->imp_at.iat_portal[j] == 0)
850 k = max_t(unsigned int, k,
851 at_get(&imp->imp_at.iat_service_estimate[j]));
853 seq_printf(m, " service_estimates:\n"
854 " services: %u sec\n"
855 " network: %d sec\n",
857 at_get(&imp->imp_at.iat_net_latency));
859 seq_printf(m, " transactions:\n"
860 " last_replay: %llu\n"
861 " peer_committed: %llu\n"
862 " last_checked: %llu\n",
863 imp->imp_last_replay_transno,
864 imp->imp_peer_committed_transno,
865 imp->imp_last_transno_checked);
868 for (rw = 0; rw <= 1; rw++) {
869 lprocfs_stats_collect(obd->obd_svc_stats,
870 PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
872 if (ret.lc_sum > 0 && ret.lc_count > 0) {
873 ret.lc_sum = div64_s64(ret.lc_sum, ret.lc_count);
874 seq_printf(m, " %s_data_averages:\n"
875 " bytes_per_rpc: %llu\n",
876 rw ? "write" : "read",
880 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
881 header = &obd->obd_svc_stats->ls_cnt_header[j];
882 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
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_per_rpc: %llu\n",
886 header->lc_units, ret.lc_sum);
889 seq_printf(m, " MB_per_sec: %u.%.02u\n",
890 k / j, (100 * k / j) % 100);
895 int lprocfs_import_seq_show(struct seq_file *m, void *data)
897 struct obd_device *obd = (struct obd_device *)data;
898 struct obd_import *imp;
901 LASSERT(obd != NULL);
902 with_imp_locked(obd, imp, rv)
903 lprocfs_import_seq_show_locked(m, obd, imp);
906 EXPORT_SYMBOL(lprocfs_import_seq_show);
908 int lprocfs_state_seq_show(struct seq_file *m, void *data)
910 struct obd_device *obd = (struct obd_device *)data;
911 struct obd_import *imp;
915 LASSERT(obd != NULL);
916 with_imp_locked(obd, imp, rc) {
917 seq_printf(m, "current_state: %s\n",
918 ptlrpc_import_state_name(imp->imp_state));
919 seq_printf(m, "state_history:\n");
920 k = imp->imp_state_hist_idx;
921 for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
922 struct import_state_hist *ish =
923 &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
924 if (ish->ish_state == 0)
926 seq_printf(m, " - [ %lld, %s ]\n", (s64)ish->ish_time,
927 ptlrpc_import_state_name(ish->ish_state));
933 EXPORT_SYMBOL(lprocfs_state_seq_show);
935 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
938 for (i = 0; i < AT_BINS; i++)
939 seq_printf(m, "%3u ", at->at_hist[i]);
943 EXPORT_SYMBOL(lprocfs_at_hist_helper);
945 /* See also ptlrpc_lprocfs_timeouts_show_seq */
946 static void lprocfs_timeouts_seq_show_locked(struct seq_file *m,
947 struct obd_device *obd,
948 struct obd_import *imp)
950 timeout_t cur_timeout, worst_timeout;
951 time64_t now, worst_timestamp;
954 LASSERT(obd != NULL);
956 now = ktime_get_real_seconds();
958 /* Some network health info for kicks */
959 seq_printf(m, "%-10s : %lld, %llds ago\n",
960 "last reply", (s64)imp->imp_last_reply_time,
961 (s64)(now - imp->imp_last_reply_time));
963 cur_timeout = at_get(&imp->imp_at.iat_net_latency);
964 worst_timeout = imp->imp_at.iat_net_latency.at_worst_timeout_ever;
965 worst_timestamp = imp->imp_at.iat_net_latency.at_worst_timestamp;
966 seq_printf(m, "%-10s : cur %3u worst %3u (at %lld, %llds ago) ",
967 "network", cur_timeout, worst_timeout, worst_timestamp,
968 now - worst_timestamp);
969 lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
971 for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
972 struct adaptive_timeout *service_est;
974 if (imp->imp_at.iat_portal[i] == 0)
977 service_est = &imp->imp_at.iat_service_estimate[i];
978 cur_timeout = at_get(service_est);
979 worst_timeout = service_est->at_worst_timeout_ever;
980 worst_timestamp = service_est->at_worst_timestamp;
981 seq_printf(m, "portal %-2d : cur %3u worst %3u (at %lld, %llds ago) ",
982 imp->imp_at.iat_portal[i], cur_timeout,
983 worst_timeout, worst_timestamp,
984 now - worst_timestamp);
985 lprocfs_at_hist_helper(m, service_est);
989 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
991 struct obd_device *obd = (struct obd_device *)data;
992 struct obd_import *imp;
995 with_imp_locked(obd, imp, rc)
996 lprocfs_timeouts_seq_show_locked(m, obd, imp);
999 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
1001 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
1003 struct obd_device *obd = data;
1006 struct obd_import *imp;
1009 with_imp_locked(obd, imp, rc) {
1010 flags = imp->imp_connect_data.ocd_connect_flags;
1011 flags2 = imp->imp_connect_data.ocd_connect_flags2;
1012 seq_printf(m, "flags=%#llx\n", flags);
1013 seq_printf(m, "flags2=%#llx\n", flags2);
1014 obd_connect_seq_flags2str(m, flags, flags2, "\n");
1015 seq_printf(m, "\n");
1020 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
1022 static const struct attribute *obd_def_uuid_attrs[] = {
1023 &lustre_attr_uuid.attr,
1027 static const struct attribute *obd_def_attrs[] = {
1028 &lustre_attr_blocksize.attr,
1029 &lustre_attr_kbytestotal.attr,
1030 &lustre_attr_kbytesfree.attr,
1031 &lustre_attr_kbytesavail.attr,
1032 &lustre_attr_filestotal.attr,
1033 &lustre_attr_filesfree.attr,
1034 &lustre_attr_uuid.attr,
1038 static void obd_sysfs_release(struct kobject *kobj)
1040 struct obd_device *obd = container_of(kobj, struct obd_device,
1043 complete(&obd->obd_kobj_unregister);
1046 int lprocfs_obd_setup(struct obd_device *obd, bool uuid_only)
1048 struct lprocfs_vars *debugfs_vars = NULL;
1051 if (!obd || obd->obd_magic != OBD_DEVICE_MAGIC)
1054 rc = kobject_set_name(&obd->obd_kset.kobj, "%s", obd->obd_name);
1058 obd->obd_ktype.sysfs_ops = &lustre_sysfs_ops;
1059 obd->obd_ktype.release = obd_sysfs_release;
1061 obd->obd_kset.kobj.parent = &obd->obd_type->typ_kobj;
1062 obd->obd_kset.kobj.ktype = &obd->obd_ktype;
1063 init_completion(&obd->obd_kobj_unregister);
1064 rc = kset_register(&obd->obd_kset);
1069 obd->obd_attrs = obd_def_uuid_attrs;
1071 obd->obd_attrs = obd_def_attrs;
1073 rc = sysfs_create_files(&obd->obd_kset.kobj, obd->obd_attrs);
1075 kset_unregister(&obd->obd_kset);
1079 if (!obd->obd_type->typ_procroot)
1080 debugfs_vars = obd->obd_vars;
1081 obd->obd_debugfs_entry = debugfs_create_dir(
1082 obd->obd_name, obd->obd_type->typ_debugfs_entry);
1083 ldebugfs_add_vars(obd->obd_debugfs_entry, debugfs_vars, obd);
1085 if (obd->obd_proc_entry || !obd->obd_type->typ_procroot)
1086 GOTO(already_registered, rc);
1088 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1089 obd->obd_type->typ_procroot,
1090 obd->obd_vars, obd);
1091 if (IS_ERR(obd->obd_proc_entry)) {
1092 rc = PTR_ERR(obd->obd_proc_entry);
1093 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1094 obd->obd_proc_entry = NULL;
1096 debugfs_remove_recursive(obd->obd_debugfs_entry);
1097 obd->obd_debugfs_entry = NULL;
1099 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1100 obd->obd_attrs = NULL;
1101 kset_unregister(&obd->obd_kset);
1107 EXPORT_SYMBOL(lprocfs_obd_setup);
1109 int lprocfs_obd_cleanup(struct obd_device *obd)
1114 if (obd->obd_proc_exports_entry) {
1115 /* Should be no exports left */
1116 lprocfs_remove(&obd->obd_proc_exports_entry);
1117 obd->obd_proc_exports_entry = NULL;
1120 if (obd->obd_proc_entry) {
1121 lprocfs_remove(&obd->obd_proc_entry);
1122 obd->obd_proc_entry = NULL;
1125 debugfs_remove_recursive(obd->obd_debugfs_entry);
1126 obd->obd_debugfs_entry = NULL;
1128 /* obd device never allocated a kset */
1129 if (!obd->obd_kset.kobj.state_initialized)
1132 if (obd->obd_attrs) {
1133 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1134 obd->obd_attrs = NULL;
1137 kset_unregister(&obd->obd_kset);
1138 wait_for_completion(&obd->obd_kobj_unregister);
1141 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1143 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1145 struct lprocfs_counter *cntr;
1146 unsigned int percpusize;
1148 unsigned long flags = 0;
1151 LASSERT(stats->ls_percpu[cpuid] == NULL);
1152 LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1154 percpusize = lprocfs_stats_counter_size(stats);
1155 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1156 if (stats->ls_percpu[cpuid]) {
1158 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1159 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1160 spin_lock_irqsave(&stats->ls_lock, flags);
1162 spin_lock(&stats->ls_lock);
1163 if (stats->ls_biggest_alloc_num <= cpuid)
1164 stats->ls_biggest_alloc_num = cpuid + 1;
1165 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1166 spin_unlock_irqrestore(&stats->ls_lock, flags);
1168 spin_unlock(&stats->ls_lock);
1171 /* initialize the ls_percpu[cpuid] non-zero counter */
1172 for (i = 0; i < stats->ls_num; ++i) {
1173 cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1174 cntr->lc_min = LC_MIN_INIT;
1180 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1181 enum lprocfs_stats_flags flags)
1183 struct lprocfs_stats *stats;
1184 unsigned int num_entry;
1185 unsigned int percpusize = 0;
1191 if (lprocfs_no_percpu_stats != 0)
1192 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1194 if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1197 num_entry = num_possible_cpus();
1199 /* alloc percpu pointers for all possible cpu slots */
1200 LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1204 stats->ls_num = num;
1205 stats->ls_flags = flags;
1206 spin_lock_init(&stats->ls_lock);
1208 /* alloc num of counter headers */
1209 CFS_ALLOC_PTR_ARRAY(stats->ls_cnt_header, stats->ls_num);
1210 if (!stats->ls_cnt_header)
1213 if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1214 /* contains only one set counters */
1215 percpusize = lprocfs_stats_counter_size(stats);
1216 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1217 if (!stats->ls_percpu[0])
1219 stats->ls_biggest_alloc_num = 1;
1220 } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1221 /* alloc all percpu data, currently only obd_memory use this */
1222 for (i = 0; i < num_entry; ++i)
1223 if (lprocfs_stats_alloc_one(stats, i) < 0)
1230 lprocfs_free_stats(&stats);
1233 EXPORT_SYMBOL(lprocfs_alloc_stats);
1235 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1237 struct lprocfs_stats *stats = *statsh;
1238 unsigned int num_entry;
1239 unsigned int percpusize;
1242 if (!stats || stats->ls_num == 0)
1246 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1249 num_entry = num_possible_cpus();
1251 percpusize = lprocfs_stats_counter_size(stats);
1252 for (i = 0; i < num_entry; i++)
1253 if (stats->ls_percpu[i])
1254 LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1255 if (stats->ls_cnt_header)
1256 CFS_FREE_PTR_ARRAY(stats->ls_cnt_header, stats->ls_num);
1257 LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1259 EXPORT_SYMBOL(lprocfs_free_stats);
1261 u64 lprocfs_stats_collector(struct lprocfs_stats *stats, int idx,
1262 enum lprocfs_fields_flags field)
1264 unsigned long flags = 0;
1265 unsigned int num_cpu;
1271 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1272 for (i = 0; i < num_cpu; i++) {
1273 struct lprocfs_counter *cntr;
1275 if (!stats->ls_percpu[i])
1278 cntr = lprocfs_stats_counter_get(stats, i, idx);
1279 ret += lprocfs_read_helper(cntr, &stats->ls_cnt_header[idx],
1280 stats->ls_flags, field);
1282 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1285 EXPORT_SYMBOL(lprocfs_stats_collector);
1287 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1289 struct lprocfs_counter *percpu_cntr;
1292 unsigned int num_entry;
1293 unsigned long flags = 0;
1295 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1297 for (i = 0; i < num_entry; i++) {
1298 if (!stats->ls_percpu[i])
1300 for (j = 0; j < stats->ls_num; j++) {
1301 percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1302 percpu_cntr->lc_count = 0;
1303 percpu_cntr->lc_min = LC_MIN_INIT;
1304 percpu_cntr->lc_max = 0;
1305 percpu_cntr->lc_sumsquare = 0;
1306 percpu_cntr->lc_sum = 0;
1307 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1308 percpu_cntr->lc_sum_irq = 0;
1312 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1314 EXPORT_SYMBOL(lprocfs_clear_stats);
1316 static ssize_t lprocfs_stats_seq_write(struct file *file,
1317 const char __user *buf,
1318 size_t len, loff_t *off)
1320 struct seq_file *seq = file->private_data;
1321 struct lprocfs_stats *stats = seq->private;
1323 lprocfs_clear_stats(stats);
1328 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1330 struct lprocfs_stats *stats = p->private;
1332 return (*pos < stats->ls_num) ? pos : NULL;
1335 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1339 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1343 return lprocfs_stats_seq_start(p, pos);
1346 /* seq file export of one lprocfs counter */
1347 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1349 struct lprocfs_stats *stats = p->private;
1350 struct lprocfs_counter_header *hdr;
1351 struct lprocfs_counter ctr;
1352 int idx = *(loff_t *)v;
1355 struct timespec64 now;
1357 ktime_get_real_ts64(&now);
1358 seq_printf(p, "%-25s %llu.%09lu secs.nsecs\n",
1359 "snapshot_time", (s64)now.tv_sec, now.tv_nsec);
1362 hdr = &stats->ls_cnt_header[idx];
1363 lprocfs_stats_collect(stats, idx, &ctr);
1365 if (ctr.lc_count == 0)
1368 seq_printf(p, "%-25s %lld samples [%s]", hdr->lc_name,
1369 ctr.lc_count, hdr->lc_units);
1371 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1372 seq_printf(p, " %lld %lld %lld",
1373 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1374 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1375 seq_printf(p, " %llu", ctr.lc_sumsquare);
1381 static const struct seq_operations lprocfs_stats_seq_sops = {
1382 .start = lprocfs_stats_seq_start,
1383 .stop = lprocfs_stats_seq_stop,
1384 .next = lprocfs_stats_seq_next,
1385 .show = lprocfs_stats_seq_show,
1388 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1390 struct seq_file *seq;
1393 rc = seq_open(file, &lprocfs_stats_seq_sops);
1396 seq = file->private_data;
1397 seq->private = inode->i_private ? inode->i_private : PDE_DATA(inode);
1401 const struct file_operations lprocfs_stats_seq_fops = {
1402 .owner = THIS_MODULE,
1403 .open = lprocfs_stats_seq_open,
1405 .write = lprocfs_stats_seq_write,
1406 .llseek = seq_lseek,
1407 .release = lprocfs_seq_release,
1409 EXPORT_SYMBOL(lprocfs_stats_seq_fops);
1411 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1412 struct lprocfs_stats *stats)
1414 struct proc_dir_entry *entry;
1415 LASSERT(root != NULL);
1417 entry = proc_create_data(name, 0644, root,
1418 &lprocfs_stats_seq_fops, stats);
1423 EXPORT_SYMBOL(lprocfs_register_stats);
1425 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1426 unsigned conf, const char *name, const char *units)
1428 struct lprocfs_counter_header *header;
1429 struct lprocfs_counter *percpu_cntr;
1430 unsigned long flags = 0;
1432 unsigned int num_cpu;
1434 LASSERT(stats != NULL);
1436 header = &stats->ls_cnt_header[index];
1437 LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1438 index, name, units);
1440 header->lc_config = conf;
1441 header->lc_name = name;
1442 header->lc_units = units;
1444 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1445 for (i = 0; i < num_cpu; ++i) {
1446 if (!stats->ls_percpu[i])
1448 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1449 percpu_cntr->lc_count = 0;
1450 percpu_cntr->lc_min = LC_MIN_INIT;
1451 percpu_cntr->lc_max = 0;
1452 percpu_cntr->lc_sumsquare = 0;
1453 percpu_cntr->lc_sum = 0;
1454 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1455 percpu_cntr->lc_sum_irq = 0;
1457 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1459 EXPORT_SYMBOL(lprocfs_counter_init);
1461 static const char * const mps_stats[] = {
1462 [LPROC_MD_CLOSE] = "close",
1463 [LPROC_MD_CREATE] = "create",
1464 [LPROC_MD_ENQUEUE] = "enqueue",
1465 [LPROC_MD_GETATTR] = "getattr",
1466 [LPROC_MD_INTENT_LOCK] = "intent_lock",
1467 [LPROC_MD_LINK] = "link",
1468 [LPROC_MD_RENAME] = "rename",
1469 [LPROC_MD_SETATTR] = "setattr",
1470 [LPROC_MD_FSYNC] = "fsync",
1471 [LPROC_MD_READ_PAGE] = "read_page",
1472 [LPROC_MD_UNLINK] = "unlink",
1473 [LPROC_MD_SETXATTR] = "setxattr",
1474 [LPROC_MD_GETXATTR] = "getxattr",
1475 [LPROC_MD_INTENT_GETATTR_ASYNC] = "intent_getattr_async",
1476 [LPROC_MD_REVALIDATE_LOCK] = "revalidate_lock",
1479 int lprocfs_alloc_md_stats(struct obd_device *obd,
1480 unsigned int num_private_stats)
1482 struct lprocfs_stats *stats;
1483 unsigned int num_stats;
1487 * TODO Ensure that this function is only used where
1488 * appropriate by adding an assertion to the effect that
1489 * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1490 * because mdt_procfs_init() uses this function to allocate
1491 * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1492 * mdt layer does not use the md_ops interface. This is
1493 * confusing and a waste of memory. See LU-2484.
1495 LASSERT(obd->obd_proc_entry != NULL);
1496 LASSERT(obd->obd_md_stats == NULL);
1498 num_stats = ARRAY_SIZE(mps_stats) + num_private_stats;
1499 stats = lprocfs_alloc_stats(num_stats, 0);
1503 for (i = 0; i < ARRAY_SIZE(mps_stats); i++) {
1504 lprocfs_counter_init(stats, i, 0, mps_stats[i], "reqs");
1505 if (!stats->ls_cnt_header[i].lc_name) {
1506 CERROR("Missing md_stat initializer md_op operation at offset %d. Aborting.\n",
1512 rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1514 lprocfs_free_stats(&stats);
1516 obd->obd_md_stats = stats;
1521 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1523 void lprocfs_free_md_stats(struct obd_device *obd)
1525 struct lprocfs_stats *stats = obd->obd_md_stats;
1528 obd->obd_md_stats = NULL;
1529 lprocfs_free_stats(&stats);
1532 EXPORT_SYMBOL(lprocfs_free_md_stats);
1534 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1536 lprocfs_counter_init(ldlm_stats,
1537 LDLM_ENQUEUE - LDLM_FIRST_OPC,
1538 0, "ldlm_enqueue", "reqs");
1539 lprocfs_counter_init(ldlm_stats,
1540 LDLM_CONVERT - LDLM_FIRST_OPC,
1541 0, "ldlm_convert", "reqs");
1542 lprocfs_counter_init(ldlm_stats,
1543 LDLM_CANCEL - LDLM_FIRST_OPC,
1544 0, "ldlm_cancel", "reqs");
1545 lprocfs_counter_init(ldlm_stats,
1546 LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1547 0, "ldlm_bl_callback", "reqs");
1548 lprocfs_counter_init(ldlm_stats,
1549 LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1550 0, "ldlm_cp_callback", "reqs");
1551 lprocfs_counter_init(ldlm_stats,
1552 LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1553 0, "ldlm_gl_callback", "reqs");
1555 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1557 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1558 struct lprocfs_counter_header *header,
1559 enum lprocfs_stats_flags flags,
1560 enum lprocfs_fields_flags field)
1568 case LPROCFS_FIELDS_FLAGS_CONFIG:
1569 ret = header->lc_config;
1571 case LPROCFS_FIELDS_FLAGS_SUM:
1573 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1574 ret += lc->lc_sum_irq;
1576 case LPROCFS_FIELDS_FLAGS_MIN:
1579 case LPROCFS_FIELDS_FLAGS_MAX:
1582 case LPROCFS_FIELDS_FLAGS_AVG:
1583 ret = (lc->lc_max - lc->lc_min) / 2;
1585 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1586 ret = lc->lc_sumsquare;
1588 case LPROCFS_FIELDS_FLAGS_COUNT:
1596 EXPORT_SYMBOL(lprocfs_read_helper);
1599 * string_to_size - convert ASCII string representing a numerical
1600 * value with optional units to 64-bit binary value
1602 * @size: The numerical value extract out of @buffer
1603 * @buffer: passed in string to parse
1604 * @count: length of the @buffer
1606 * This function returns a 64-bit binary value if @buffer contains a valid
1607 * numerical string. The string is parsed to 3 significant figures after
1608 * the decimal point. Support the string containing an optional units at
1609 * the end which can be base 2 or base 10 in value. If no units are given
1610 * the string is assumed to just a numerical value.
1612 * Returns: @count if the string is successfully parsed,
1613 * -errno on invalid input strings. Error values:
1615 * - ``-EINVAL``: @buffer is not a proper numerical string
1616 * - ``-EOVERFLOW``: results does not fit into 64 bits.
1617 * - ``-E2BIG ``: @buffer is too large (not a valid number)
1619 int string_to_size(u64 *size, const char *buffer, size_t count)
1621 /* For string_get_size() it can support values above exabytes,
1622 * (ZiB, YiB) due to breaking the return value into a size and
1623 * bulk size to avoid 64 bit overflow. We don't break the size
1624 * up into block size units so we don't support ZiB or YiB.
1626 static const char *const units_10[] = {
1627 "kB", "MB", "GB", "TB", "PB", "EB",
1629 static const char *const units_2[] = {
1630 "K", "M", "G", "T", "P", "E",
1632 static const char *const *const units_str[] = {
1633 [STRING_UNITS_2] = units_2,
1634 [STRING_UNITS_10] = units_10,
1636 static const unsigned int coeff[] = {
1637 [STRING_UNITS_10] = 1000,
1638 [STRING_UNITS_2] = 1024,
1640 enum string_size_units unit = STRING_UNITS_2;
1641 u64 whole, blk_size = 1;
1642 char kernbuf[22], *end;
1647 if (count >= sizeof(kernbuf)) {
1648 CERROR("count %zd > buffer %zd\n", count, sizeof(kernbuf));
1653 /* The "iB" suffix is optionally allowed for indicating base-2 numbers.
1654 * If suffix is only "B" and not "iB" then we treat it as base-10.
1656 end = strstr(buffer, "B");
1657 if (end && *(end - 1) != 'i')
1658 unit = STRING_UNITS_10;
1660 i = unit == STRING_UNITS_2 ? ARRAY_SIZE(units_2) - 1 :
1661 ARRAY_SIZE(units_10) - 1;
1663 end = strnstr(buffer, units_str[unit][i], count);
1666 blk_size *= coeff[unit];
1672 /* as 'B' is a substring of all units, we need to handle it
1676 /* 'B' is only acceptable letter at this point */
1677 end = strnchr(buffer, count, 'B');
1681 if (count - len > 2 ||
1682 (count - len == 2 && strcmp(end, "B\n") != 0)) {
1683 CDEBUG(D_INFO, "unknown suffix '%s'\n", buffer);
1687 /* kstrtoull will error out if it has non digits */
1691 end = strnchr(buffer, count, '.');
1693 /* need to limit 3 decimal places */
1694 char rem[4] = "000";
1701 /* limit to 3 decimal points */
1702 off = min_t(size_t, 3, strspn(end, "0123456789"));
1703 /* need to limit frac_d to a u32 */
1704 memcpy(rem, end, off);
1705 rc = kstrtoull(rem, 10, &frac);
1709 if (fls64(frac) + fls64(blk_size) - 1 > 64)
1717 snprintf(kernbuf, sizeof(kernbuf), "%.*s", (int)len, buffer);
1718 rc = kstrtoull(kernbuf, 10, &whole);
1722 if (whole != 0 && fls64(whole) + fls64(blk_size) - 1 > 64)
1725 *size += whole * blk_size;
1729 EXPORT_SYMBOL(string_to_size);
1732 * sysfs_memparse - parse a ASCII string to 64-bit binary value,
1733 * with optional units
1735 * @buffer: kernel pointer to input string
1736 * @count: number of bytes in the input @buffer
1737 * @val: (output) binary value returned to caller
1738 * @defunit: default unit suffix to use if none is provided
1740 * Parses a string into a number. The number stored at @buffer is
1741 * potentially suffixed with K, M, G, T, P, E. Besides these other
1742 * valid suffix units are shown in the string_to_size() function.
1743 * If the string lacks a suffix then the defunit is used. The defunit
1744 * should be given as a binary unit (e.g. MiB) as that is the standard
1745 * for tunables in Lustre. If no unit suffix is given (e.g. 'G'), then
1746 * it is assumed to be in binary units.
1748 * Returns: 0 on success or -errno on failure.
1750 int sysfs_memparse(const char *buffer, size_t count, u64 *val,
1751 const char *defunit)
1753 const char *param = buffer;
1757 count = strlen(buffer);
1758 while (count > 0 && isspace(buffer[count - 1]))
1764 /* If there isn't already a unit on this value, append @defunit.
1765 * Units of 'B' don't affect the value, so don't bother adding.
1767 if (!isalpha(buffer[count - 1]) && defunit[0] != 'B') {
1768 if (count + 3 >= sizeof(tmp_buf)) {
1769 CERROR("count %zd > size %zd\n", count, sizeof(param));
1773 scnprintf(tmp_buf, sizeof(tmp_buf), "%.*s%s", (int)count,
1776 count = strlen(param);
1779 rc = string_to_size(val, param, count);
1781 return rc < 0 ? rc : 0;
1783 EXPORT_SYMBOL(sysfs_memparse);
1785 char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1794 if (!memcmp(s1, s2, l2))
1800 EXPORT_SYMBOL(lprocfs_strnstr);
1803 * Find the string \a name in the input \a buffer, and return a pointer to the
1804 * value immediately following \a name, reducing \a count appropriately.
1805 * If \a name is not found the original \a buffer is returned.
1807 char *lprocfs_find_named_value(const char *buffer, const char *name,
1811 size_t buflen = *count;
1813 /* there is no strnstr() in rhel5 and ubuntu kernels */
1814 val = lprocfs_strnstr(buffer, name, buflen);
1816 return (char *)buffer;
1818 val += strlen(name); /* skip prefix */
1819 while (val < buffer + buflen && isspace(*val)) /* skip separator */
1823 while (val < buffer + buflen && isalnum(*val)) {
1828 return val - *count;
1830 EXPORT_SYMBOL(lprocfs_find_named_value);
1832 int lprocfs_seq_create(struct proc_dir_entry *parent,
1835 const struct file_operations *seq_fops,
1838 struct proc_dir_entry *entry;
1841 /* Disallow secretly (un)writable entries. */
1842 LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
1844 entry = proc_create_data(name, mode, parent, seq_fops, data);
1851 EXPORT_SYMBOL(lprocfs_seq_create);
1853 int lprocfs_obd_seq_create(struct obd_device *obd,
1856 const struct file_operations *seq_fops,
1859 return lprocfs_seq_create(obd->obd_proc_entry, name,
1860 mode, seq_fops, data);
1862 EXPORT_SYMBOL(lprocfs_obd_seq_create);
1864 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
1866 if (value >= OBD_HIST_MAX)
1867 value = OBD_HIST_MAX - 1;
1869 spin_lock(&oh->oh_lock);
1870 oh->oh_buckets[value]++;
1871 spin_unlock(&oh->oh_lock);
1873 EXPORT_SYMBOL(lprocfs_oh_tally);
1875 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
1877 unsigned int val = 0;
1879 if (likely(value != 0))
1880 val = min(fls(value - 1), OBD_HIST_MAX);
1882 lprocfs_oh_tally(oh, val);
1884 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
1886 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
1888 unsigned long ret = 0;
1891 for (i = 0; i < OBD_HIST_MAX; i++)
1892 ret += oh->oh_buckets[i];
1895 EXPORT_SYMBOL(lprocfs_oh_sum);
1897 void lprocfs_oh_clear(struct obd_histogram *oh)
1899 spin_lock(&oh->oh_lock);
1900 memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
1901 spin_unlock(&oh->oh_lock);
1903 EXPORT_SYMBOL(lprocfs_oh_clear);
1905 ssize_t lustre_attr_show(struct kobject *kobj,
1906 struct attribute *attr, char *buf)
1908 struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1910 return a->show ? a->show(kobj, attr, buf) : 0;
1912 EXPORT_SYMBOL_GPL(lustre_attr_show);
1914 ssize_t lustre_attr_store(struct kobject *kobj, struct attribute *attr,
1915 const char *buf, size_t len)
1917 struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1919 return a->store ? a->store(kobj, attr, buf, len) : len;
1921 EXPORT_SYMBOL_GPL(lustre_attr_store);
1923 const struct sysfs_ops lustre_sysfs_ops = {
1924 .show = lustre_attr_show,
1925 .store = lustre_attr_store,
1927 EXPORT_SYMBOL_GPL(lustre_sysfs_ops);
1929 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
1931 struct obd_device *obd = data;
1932 struct client_obd *cli = &obd->u.cli;
1934 spin_lock(&cli->cl_loi_list_lock);
1935 seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
1936 spin_unlock(&cli->cl_loi_list_lock);
1939 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
1941 ssize_t lprocfs_obd_max_pages_per_rpc_seq_write(struct file *file,
1942 const char __user *buffer,
1943 size_t count, loff_t *off)
1945 struct seq_file *m = file->private_data;
1946 struct obd_device *obd = m->private;
1947 struct client_obd *cli = &obd->u.cli;
1948 struct obd_import *imp;
1949 struct obd_connect_data *ocd;
1954 if (count > sizeof(kernbuf) - 1)
1957 if (copy_from_user(kernbuf, buffer, count))
1960 kernbuf[count] = '\0';
1962 rc = sysfs_memparse(kernbuf, count, &val, "B");
1966 /* if the max_pages is specified in bytes, convert to pages */
1967 if (val >= ONE_MB_BRW_SIZE)
1970 with_imp_locked(obd, imp, rc) {
1971 ocd = &imp->imp_connect_data;
1972 chunk_mask = ~((1 << (cli->cl_chunkbits - PAGE_SHIFT)) - 1);
1973 /* max_pages_per_rpc must be chunk aligned */
1974 val = (val + ~chunk_mask) & chunk_mask;
1975 if (val == 0 || (ocd->ocd_brw_size != 0 &&
1976 val > ocd->ocd_brw_size >> PAGE_SHIFT)) {
1979 spin_lock(&cli->cl_loi_list_lock);
1980 cli->cl_max_pages_per_rpc = val;
1981 client_adjust_max_dirty(cli);
1982 spin_unlock(&cli->cl_loi_list_lock);
1988 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_write);
1990 ssize_t short_io_bytes_show(struct kobject *kobj, struct attribute *attr,
1993 struct obd_device *obd = container_of(kobj, struct obd_device,
1995 struct client_obd *cli = &obd->u.cli;
1998 spin_lock(&cli->cl_loi_list_lock);
1999 rc = sprintf(buf, "%d\n", cli->cl_max_short_io_bytes);
2000 spin_unlock(&cli->cl_loi_list_lock);
2003 EXPORT_SYMBOL(short_io_bytes_show);
2005 /* Used to catch people who think they're specifying pages. */
2006 #define MIN_SHORT_IO_BYTES 64U
2008 ssize_t short_io_bytes_store(struct kobject *kobj, struct attribute *attr,
2009 const char *buffer, size_t count)
2011 struct obd_device *obd = container_of(kobj, struct obd_device,
2013 struct client_obd *cli = &obd->u.cli;
2017 if (strcmp(buffer, "-1") == 0) {
2018 val = OBD_DEF_SHORT_IO_BYTES;
2020 rc = sysfs_memparse(buffer, count, &val, "B");
2025 if (val && (val < MIN_SHORT_IO_BYTES || val > LNET_MTU))
2026 GOTO(out, rc = -ERANGE);
2030 spin_lock(&cli->cl_loi_list_lock);
2031 cli->cl_max_short_io_bytes = min_t(u64, val, OST_MAX_SHORT_IO_BYTES);
2032 spin_unlock(&cli->cl_loi_list_lock);
2037 EXPORT_SYMBOL(short_io_bytes_store);
2039 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2040 struct root_squash_info *squash, char *name)
2043 char kernbuf[64], *tmp, *errmsg;
2044 unsigned long uid, gid;
2047 if (count >= sizeof(kernbuf)) {
2048 errmsg = "string too long";
2049 GOTO(failed_noprint, rc = -EINVAL);
2051 if (copy_from_user(kernbuf, buffer, count)) {
2052 errmsg = "bad address";
2053 GOTO(failed_noprint, rc = -EFAULT);
2055 kernbuf[count] = '\0';
2057 /* look for uid gid separator */
2058 tmp = strchr(kernbuf, ':');
2060 errmsg = "needs uid:gid format";
2061 GOTO(failed, rc = -EINVAL);
2067 if (kstrtoul(kernbuf, 0, &uid) != 0) {
2069 GOTO(failed, rc = -EINVAL);
2073 if (kstrtoul(tmp, 0, &gid) != 0) {
2075 GOTO(failed, rc = -EINVAL);
2078 squash->rsi_uid = uid;
2079 squash->rsi_gid = gid;
2081 LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2082 name, squash->rsi_uid, squash->rsi_gid);
2090 CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2091 name, kernbuf, errmsg, rc);
2094 CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2098 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2101 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2102 struct root_squash_info *squash, char *name)
2105 char *kernbuf = NULL;
2112 errmsg = "string too long";
2113 GOTO(failed, rc = -EINVAL);
2116 OBD_ALLOC(kernbuf, count + 1);
2118 errmsg = "no memory";
2119 GOTO(failed, rc = -ENOMEM);
2121 if (copy_from_user(kernbuf, buffer, count)) {
2122 errmsg = "bad address";
2123 GOTO(failed, rc = -EFAULT);
2125 kernbuf[count] = '\0';
2127 if (count > 0 && kernbuf[count - 1] == '\n')
2130 if ((len == 4 && strncmp(kernbuf, "NONE", len) == 0) ||
2131 (len == 5 && strncmp(kernbuf, "clear", len) == 0)) {
2132 /* empty string is special case */
2133 spin_lock(&squash->rsi_lock);
2134 if (!list_empty(&squash->rsi_nosquash_nids))
2135 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2136 spin_unlock(&squash->rsi_lock);
2137 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2138 OBD_FREE(kernbuf, count + 1);
2142 if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2143 errmsg = "can't parse";
2144 GOTO(failed, rc = -EINVAL);
2146 LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2148 OBD_FREE(kernbuf, count + 1);
2151 spin_lock(&squash->rsi_lock);
2152 if (!list_empty(&squash->rsi_nosquash_nids))
2153 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2154 list_splice(&tmp, &squash->rsi_nosquash_nids);
2155 spin_unlock(&squash->rsi_lock);
2161 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2162 name, kernbuf, errmsg, rc);
2163 OBD_FREE(kernbuf, count + 1);
2165 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2170 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2172 #endif /* CONFIG_PROC_FS*/