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: %u 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 unsigned int cur, worst;
951 time64_t now, worstt;
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 = at_get(&imp->imp_at.iat_net_latency);
964 worst = imp->imp_at.iat_net_latency.at_worst_ever;
965 worstt = imp->imp_at.iat_net_latency.at_worst_time;
966 seq_printf(m, "%-10s : cur %3u worst %3u (at %lld, %llds ago) ",
967 "network", cur, worst, (s64)worstt, (s64)(now - worstt));
968 lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
970 for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
971 if (imp->imp_at.iat_portal[i] == 0)
973 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
974 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
975 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
976 seq_printf(m, "portal %-2d : cur %3u worst %3u (at %lld, %llds ago) ",
977 imp->imp_at.iat_portal[i], cur, worst, (s64)worstt,
978 (s64)(now - worstt));
979 lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
983 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
985 struct obd_device *obd = (struct obd_device *)data;
986 struct obd_import *imp;
989 with_imp_locked(obd, imp, rc)
990 lprocfs_timeouts_seq_show_locked(m, obd, imp);
993 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
995 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
997 struct obd_device *obd = data;
1000 struct obd_import *imp;
1003 with_imp_locked(obd, imp, rc) {
1004 flags = imp->imp_connect_data.ocd_connect_flags;
1005 flags2 = imp->imp_connect_data.ocd_connect_flags2;
1006 seq_printf(m, "flags=%#llx\n", flags);
1007 seq_printf(m, "flags2=%#llx\n", flags2);
1008 obd_connect_seq_flags2str(m, flags, flags2, "\n");
1009 seq_printf(m, "\n");
1014 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
1016 static const struct attribute *obd_def_uuid_attrs[] = {
1017 &lustre_attr_uuid.attr,
1021 static const struct attribute *obd_def_attrs[] = {
1022 &lustre_attr_blocksize.attr,
1023 &lustre_attr_kbytestotal.attr,
1024 &lustre_attr_kbytesfree.attr,
1025 &lustre_attr_kbytesavail.attr,
1026 &lustre_attr_filestotal.attr,
1027 &lustre_attr_filesfree.attr,
1028 &lustre_attr_uuid.attr,
1032 static void obd_sysfs_release(struct kobject *kobj)
1034 struct obd_device *obd = container_of(kobj, struct obd_device,
1037 complete(&obd->obd_kobj_unregister);
1040 int lprocfs_obd_setup(struct obd_device *obd, bool uuid_only)
1042 struct lprocfs_vars *debugfs_vars = NULL;
1045 if (!obd || obd->obd_magic != OBD_DEVICE_MAGIC)
1048 rc = kobject_set_name(&obd->obd_kset.kobj, "%s", obd->obd_name);
1052 obd->obd_ktype.sysfs_ops = &lustre_sysfs_ops;
1053 obd->obd_ktype.release = obd_sysfs_release;
1055 obd->obd_kset.kobj.parent = &obd->obd_type->typ_kobj;
1056 obd->obd_kset.kobj.ktype = &obd->obd_ktype;
1057 init_completion(&obd->obd_kobj_unregister);
1058 rc = kset_register(&obd->obd_kset);
1063 obd->obd_attrs = obd_def_uuid_attrs;
1065 obd->obd_attrs = obd_def_attrs;
1067 rc = sysfs_create_files(&obd->obd_kset.kobj, obd->obd_attrs);
1069 kset_unregister(&obd->obd_kset);
1073 if (!obd->obd_type->typ_procroot)
1074 debugfs_vars = obd->obd_vars;
1075 obd->obd_debugfs_entry = debugfs_create_dir(
1076 obd->obd_name, obd->obd_type->typ_debugfs_entry);
1077 ldebugfs_add_vars(obd->obd_debugfs_entry, debugfs_vars, obd);
1079 if (obd->obd_proc_entry || !obd->obd_type->typ_procroot)
1080 GOTO(already_registered, rc);
1082 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1083 obd->obd_type->typ_procroot,
1084 obd->obd_vars, obd);
1085 if (IS_ERR(obd->obd_proc_entry)) {
1086 rc = PTR_ERR(obd->obd_proc_entry);
1087 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1088 obd->obd_proc_entry = NULL;
1090 debugfs_remove_recursive(obd->obd_debugfs_entry);
1091 obd->obd_debugfs_entry = NULL;
1093 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1094 obd->obd_attrs = NULL;
1095 kset_unregister(&obd->obd_kset);
1101 EXPORT_SYMBOL(lprocfs_obd_setup);
1103 int lprocfs_obd_cleanup(struct obd_device *obd)
1108 if (obd->obd_proc_exports_entry) {
1109 /* Should be no exports left */
1110 lprocfs_remove(&obd->obd_proc_exports_entry);
1111 obd->obd_proc_exports_entry = NULL;
1114 if (obd->obd_proc_entry) {
1115 lprocfs_remove(&obd->obd_proc_entry);
1116 obd->obd_proc_entry = NULL;
1119 debugfs_remove_recursive(obd->obd_debugfs_entry);
1120 obd->obd_debugfs_entry = NULL;
1122 /* obd device never allocated a kset */
1123 if (!obd->obd_kset.kobj.state_initialized)
1126 if (obd->obd_attrs) {
1127 sysfs_remove_files(&obd->obd_kset.kobj, obd->obd_attrs);
1128 obd->obd_attrs = NULL;
1131 kset_unregister(&obd->obd_kset);
1132 wait_for_completion(&obd->obd_kobj_unregister);
1135 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1137 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1139 struct lprocfs_counter *cntr;
1140 unsigned int percpusize;
1142 unsigned long flags = 0;
1145 LASSERT(stats->ls_percpu[cpuid] == NULL);
1146 LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1148 percpusize = lprocfs_stats_counter_size(stats);
1149 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1150 if (stats->ls_percpu[cpuid]) {
1152 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1153 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1154 spin_lock_irqsave(&stats->ls_lock, flags);
1156 spin_lock(&stats->ls_lock);
1157 if (stats->ls_biggest_alloc_num <= cpuid)
1158 stats->ls_biggest_alloc_num = cpuid + 1;
1159 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1160 spin_unlock_irqrestore(&stats->ls_lock, flags);
1162 spin_unlock(&stats->ls_lock);
1165 /* initialize the ls_percpu[cpuid] non-zero counter */
1166 for (i = 0; i < stats->ls_num; ++i) {
1167 cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1168 cntr->lc_min = LC_MIN_INIT;
1174 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1175 enum lprocfs_stats_flags flags)
1177 struct lprocfs_stats *stats;
1178 unsigned int num_entry;
1179 unsigned int percpusize = 0;
1185 if (lprocfs_no_percpu_stats != 0)
1186 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1188 if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1191 num_entry = num_possible_cpus();
1193 /* alloc percpu pointers for all possible cpu slots */
1194 LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1198 stats->ls_num = num;
1199 stats->ls_flags = flags;
1200 spin_lock_init(&stats->ls_lock);
1202 /* alloc num of counter headers */
1203 CFS_ALLOC_PTR_ARRAY(stats->ls_cnt_header, stats->ls_num);
1204 if (!stats->ls_cnt_header)
1207 if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1208 /* contains only one set counters */
1209 percpusize = lprocfs_stats_counter_size(stats);
1210 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1211 if (!stats->ls_percpu[0])
1213 stats->ls_biggest_alloc_num = 1;
1214 } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1215 /* alloc all percpu data, currently only obd_memory use this */
1216 for (i = 0; i < num_entry; ++i)
1217 if (lprocfs_stats_alloc_one(stats, i) < 0)
1224 lprocfs_free_stats(&stats);
1227 EXPORT_SYMBOL(lprocfs_alloc_stats);
1229 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1231 struct lprocfs_stats *stats = *statsh;
1232 unsigned int num_entry;
1233 unsigned int percpusize;
1236 if (!stats || stats->ls_num == 0)
1240 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1243 num_entry = num_possible_cpus();
1245 percpusize = lprocfs_stats_counter_size(stats);
1246 for (i = 0; i < num_entry; i++)
1247 if (stats->ls_percpu[i])
1248 LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1249 if (stats->ls_cnt_header)
1250 CFS_FREE_PTR_ARRAY(stats->ls_cnt_header, stats->ls_num);
1251 LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1253 EXPORT_SYMBOL(lprocfs_free_stats);
1255 u64 lprocfs_stats_collector(struct lprocfs_stats *stats, int idx,
1256 enum lprocfs_fields_flags field)
1258 unsigned long flags = 0;
1259 unsigned int num_cpu;
1265 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1266 for (i = 0; i < num_cpu; i++) {
1267 struct lprocfs_counter *cntr;
1269 if (!stats->ls_percpu[i])
1272 cntr = lprocfs_stats_counter_get(stats, i, idx);
1273 ret += lprocfs_read_helper(cntr, &stats->ls_cnt_header[idx],
1274 stats->ls_flags, field);
1276 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1279 EXPORT_SYMBOL(lprocfs_stats_collector);
1281 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1283 struct lprocfs_counter *percpu_cntr;
1286 unsigned int num_entry;
1287 unsigned long flags = 0;
1289 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1291 for (i = 0; i < num_entry; i++) {
1292 if (!stats->ls_percpu[i])
1294 for (j = 0; j < stats->ls_num; j++) {
1295 percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1296 percpu_cntr->lc_count = 0;
1297 percpu_cntr->lc_min = LC_MIN_INIT;
1298 percpu_cntr->lc_max = 0;
1299 percpu_cntr->lc_sumsquare = 0;
1300 percpu_cntr->lc_sum = 0;
1301 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1302 percpu_cntr->lc_sum_irq = 0;
1306 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1308 EXPORT_SYMBOL(lprocfs_clear_stats);
1310 static ssize_t lprocfs_stats_seq_write(struct file *file,
1311 const char __user *buf,
1312 size_t len, loff_t *off)
1314 struct seq_file *seq = file->private_data;
1315 struct lprocfs_stats *stats = seq->private;
1317 lprocfs_clear_stats(stats);
1322 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1324 struct lprocfs_stats *stats = p->private;
1326 return (*pos < stats->ls_num) ? pos : NULL;
1329 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1333 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1337 return lprocfs_stats_seq_start(p, pos);
1340 /* seq file export of one lprocfs counter */
1341 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1343 struct lprocfs_stats *stats = p->private;
1344 struct lprocfs_counter_header *hdr;
1345 struct lprocfs_counter ctr;
1346 int idx = *(loff_t *)v;
1349 struct timespec64 now;
1351 ktime_get_real_ts64(&now);
1352 seq_printf(p, "%-25s %llu.%09lu secs.nsecs\n",
1353 "snapshot_time", (s64)now.tv_sec, now.tv_nsec);
1356 hdr = &stats->ls_cnt_header[idx];
1357 lprocfs_stats_collect(stats, idx, &ctr);
1359 if (ctr.lc_count == 0)
1362 seq_printf(p, "%-25s %lld samples [%s]", hdr->lc_name,
1363 ctr.lc_count, hdr->lc_units);
1365 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1366 seq_printf(p, " %lld %lld %lld",
1367 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1368 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1369 seq_printf(p, " %llu", ctr.lc_sumsquare);
1375 static const struct seq_operations lprocfs_stats_seq_sops = {
1376 .start = lprocfs_stats_seq_start,
1377 .stop = lprocfs_stats_seq_stop,
1378 .next = lprocfs_stats_seq_next,
1379 .show = lprocfs_stats_seq_show,
1382 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1384 struct seq_file *seq;
1387 rc = seq_open(file, &lprocfs_stats_seq_sops);
1390 seq = file->private_data;
1391 seq->private = inode->i_private ? inode->i_private : PDE_DATA(inode);
1395 const struct file_operations lprocfs_stats_seq_fops = {
1396 .owner = THIS_MODULE,
1397 .open = lprocfs_stats_seq_open,
1399 .write = lprocfs_stats_seq_write,
1400 .llseek = seq_lseek,
1401 .release = lprocfs_seq_release,
1403 EXPORT_SYMBOL(lprocfs_stats_seq_fops);
1405 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1406 struct lprocfs_stats *stats)
1408 struct proc_dir_entry *entry;
1409 LASSERT(root != NULL);
1411 entry = proc_create_data(name, 0644, root,
1412 &lprocfs_stats_seq_fops, stats);
1417 EXPORT_SYMBOL(lprocfs_register_stats);
1419 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1420 unsigned conf, const char *name, const char *units)
1422 struct lprocfs_counter_header *header;
1423 struct lprocfs_counter *percpu_cntr;
1424 unsigned long flags = 0;
1426 unsigned int num_cpu;
1428 LASSERT(stats != NULL);
1430 header = &stats->ls_cnt_header[index];
1431 LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1432 index, name, units);
1434 header->lc_config = conf;
1435 header->lc_name = name;
1436 header->lc_units = units;
1438 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1439 for (i = 0; i < num_cpu; ++i) {
1440 if (!stats->ls_percpu[i])
1442 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1443 percpu_cntr->lc_count = 0;
1444 percpu_cntr->lc_min = LC_MIN_INIT;
1445 percpu_cntr->lc_max = 0;
1446 percpu_cntr->lc_sumsquare = 0;
1447 percpu_cntr->lc_sum = 0;
1448 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1449 percpu_cntr->lc_sum_irq = 0;
1451 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1453 EXPORT_SYMBOL(lprocfs_counter_init);
1455 static const char * const mps_stats[] = {
1456 [LPROC_MD_CLOSE] = "close",
1457 [LPROC_MD_CREATE] = "create",
1458 [LPROC_MD_ENQUEUE] = "enqueue",
1459 [LPROC_MD_GETATTR] = "getattr",
1460 [LPROC_MD_INTENT_LOCK] = "intent_lock",
1461 [LPROC_MD_LINK] = "link",
1462 [LPROC_MD_RENAME] = "rename",
1463 [LPROC_MD_SETATTR] = "setattr",
1464 [LPROC_MD_FSYNC] = "fsync",
1465 [LPROC_MD_READ_PAGE] = "read_page",
1466 [LPROC_MD_UNLINK] = "unlink",
1467 [LPROC_MD_SETXATTR] = "setxattr",
1468 [LPROC_MD_GETXATTR] = "getxattr",
1469 [LPROC_MD_INTENT_GETATTR_ASYNC] = "intent_getattr_async",
1470 [LPROC_MD_REVALIDATE_LOCK] = "revalidate_lock",
1473 int lprocfs_alloc_md_stats(struct obd_device *obd,
1474 unsigned int num_private_stats)
1476 struct lprocfs_stats *stats;
1477 unsigned int num_stats;
1481 * TODO Ensure that this function is only used where
1482 * appropriate by adding an assertion to the effect that
1483 * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1484 * because mdt_procfs_init() uses this function to allocate
1485 * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1486 * mdt layer does not use the md_ops interface. This is
1487 * confusing and a waste of memory. See LU-2484.
1489 LASSERT(obd->obd_proc_entry != NULL);
1490 LASSERT(obd->obd_md_stats == NULL);
1492 num_stats = ARRAY_SIZE(mps_stats) + num_private_stats;
1493 stats = lprocfs_alloc_stats(num_stats, 0);
1497 for (i = 0; i < ARRAY_SIZE(mps_stats); i++) {
1498 lprocfs_counter_init(stats, i, 0, mps_stats[i], "reqs");
1499 if (!stats->ls_cnt_header[i].lc_name) {
1500 CERROR("Missing md_stat initializer md_op operation at offset %d. Aborting.\n",
1506 rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1508 lprocfs_free_stats(&stats);
1510 obd->obd_md_stats = stats;
1515 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1517 void lprocfs_free_md_stats(struct obd_device *obd)
1519 struct lprocfs_stats *stats = obd->obd_md_stats;
1522 obd->obd_md_stats = NULL;
1523 lprocfs_free_stats(&stats);
1526 EXPORT_SYMBOL(lprocfs_free_md_stats);
1528 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1530 lprocfs_counter_init(ldlm_stats,
1531 LDLM_ENQUEUE - LDLM_FIRST_OPC,
1532 0, "ldlm_enqueue", "reqs");
1533 lprocfs_counter_init(ldlm_stats,
1534 LDLM_CONVERT - LDLM_FIRST_OPC,
1535 0, "ldlm_convert", "reqs");
1536 lprocfs_counter_init(ldlm_stats,
1537 LDLM_CANCEL - LDLM_FIRST_OPC,
1538 0, "ldlm_cancel", "reqs");
1539 lprocfs_counter_init(ldlm_stats,
1540 LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1541 0, "ldlm_bl_callback", "reqs");
1542 lprocfs_counter_init(ldlm_stats,
1543 LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1544 0, "ldlm_cp_callback", "reqs");
1545 lprocfs_counter_init(ldlm_stats,
1546 LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1547 0, "ldlm_gl_callback", "reqs");
1549 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1551 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1552 struct lprocfs_counter_header *header,
1553 enum lprocfs_stats_flags flags,
1554 enum lprocfs_fields_flags field)
1562 case LPROCFS_FIELDS_FLAGS_CONFIG:
1563 ret = header->lc_config;
1565 case LPROCFS_FIELDS_FLAGS_SUM:
1567 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1568 ret += lc->lc_sum_irq;
1570 case LPROCFS_FIELDS_FLAGS_MIN:
1573 case LPROCFS_FIELDS_FLAGS_MAX:
1576 case LPROCFS_FIELDS_FLAGS_AVG:
1577 ret = (lc->lc_max - lc->lc_min) / 2;
1579 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1580 ret = lc->lc_sumsquare;
1582 case LPROCFS_FIELDS_FLAGS_COUNT:
1590 EXPORT_SYMBOL(lprocfs_read_helper);
1593 * string_to_size - convert ASCII string representing a numerical
1594 * value with optional units to 64-bit binary value
1596 * @size: The numerical value extract out of @buffer
1597 * @buffer: passed in string to parse
1598 * @count: length of the @buffer
1600 * This function returns a 64-bit binary value if @buffer contains a valid
1601 * numerical string. The string is parsed to 3 significant figures after
1602 * the decimal point. Support the string containing an optional units at
1603 * the end which can be base 2 or base 10 in value. If no units are given
1604 * the string is assumed to just a numerical value.
1606 * Returns: @count if the string is successfully parsed,
1607 * -errno on invalid input strings. Error values:
1609 * - ``-EINVAL``: @buffer is not a proper numerical string
1610 * - ``-EOVERFLOW``: results does not fit into 64 bits.
1611 * - ``-E2BIG ``: @buffer is too large (not a valid number)
1613 int string_to_size(u64 *size, const char *buffer, size_t count)
1615 /* For string_get_size() it can support values above exabytes,
1616 * (ZiB, YiB) due to breaking the return value into a size and
1617 * bulk size to avoid 64 bit overflow. We don't break the size
1618 * up into block size units so we don't support ZiB or YiB.
1620 static const char *const units_10[] = {
1621 "kB", "MB", "GB", "TB", "PB", "EB",
1623 static const char *const units_2[] = {
1624 "K", "M", "G", "T", "P", "E",
1626 static const char *const *const units_str[] = {
1627 [STRING_UNITS_2] = units_2,
1628 [STRING_UNITS_10] = units_10,
1630 static const unsigned int coeff[] = {
1631 [STRING_UNITS_10] = 1000,
1632 [STRING_UNITS_2] = 1024,
1634 enum string_size_units unit = STRING_UNITS_2;
1635 u64 whole, blk_size = 1;
1636 char kernbuf[22], *end;
1641 if (count >= sizeof(kernbuf)) {
1642 CERROR("count %zd > buffer %zd\n", count, sizeof(kernbuf));
1647 /* The "iB" suffix is optionally allowed for indicating base-2 numbers.
1648 * If suffix is only "B" and not "iB" then we treat it as base-10.
1650 end = strstr(buffer, "B");
1651 if (end && *(end - 1) != 'i')
1652 unit = STRING_UNITS_10;
1654 i = unit == STRING_UNITS_2 ? ARRAY_SIZE(units_2) - 1 :
1655 ARRAY_SIZE(units_10) - 1;
1657 end = strnstr(buffer, units_str[unit][i], count);
1660 blk_size *= coeff[unit];
1666 /* as 'B' is a substring of all units, we need to handle it
1670 /* 'B' is only acceptable letter at this point */
1671 end = strnchr(buffer, count, 'B');
1675 if (count - len > 2 ||
1676 (count - len == 2 && strcmp(end, "B\n") != 0)) {
1677 CDEBUG(D_INFO, "unknown suffix '%s'\n", buffer);
1681 /* kstrtoull will error out if it has non digits */
1685 end = strnchr(buffer, count, '.');
1687 /* need to limit 3 decimal places */
1688 char rem[4] = "000";
1695 /* limit to 3 decimal points */
1696 off = min_t(size_t, 3, strspn(end, "0123456789"));
1697 /* need to limit frac_d to a u32 */
1698 memcpy(rem, end, off);
1699 rc = kstrtoull(rem, 10, &frac);
1703 if (fls64(frac) + fls64(blk_size) - 1 > 64)
1711 snprintf(kernbuf, sizeof(kernbuf), "%.*s", (int)len, buffer);
1712 rc = kstrtoull(kernbuf, 10, &whole);
1716 if (whole != 0 && fls64(whole) + fls64(blk_size) - 1 > 64)
1719 *size += whole * blk_size;
1723 EXPORT_SYMBOL(string_to_size);
1726 * sysfs_memparse - parse a ASCII string to 64-bit binary value,
1727 * with optional units
1729 * @buffer: kernel pointer to input string
1730 * @count: number of bytes in the input @buffer
1731 * @val: (output) binary value returned to caller
1732 * @defunit: default unit suffix to use if none is provided
1734 * Parses a string into a number. The number stored at @buffer is
1735 * potentially suffixed with K, M, G, T, P, E. Besides these other
1736 * valid suffix units are shown in the string_to_size() function.
1737 * If the string lacks a suffix then the defunit is used. The defunit
1738 * should be given as a binary unit (e.g. MiB) as that is the standard
1739 * for tunables in Lustre. If no unit suffix is given (e.g. 'G'), then
1740 * it is assumed to be in binary units.
1742 * Returns: 0 on success or -errno on failure.
1744 int sysfs_memparse(const char *buffer, size_t count, u64 *val,
1745 const char *defunit)
1747 const char *param = buffer;
1751 count = strlen(buffer);
1752 while (count > 0 && isspace(buffer[count - 1]))
1758 /* If there isn't already a unit on this value, append @defunit.
1759 * Units of 'B' don't affect the value, so don't bother adding.
1761 if (!isalpha(buffer[count - 1]) && defunit[0] != 'B') {
1762 if (count + 3 >= sizeof(tmp_buf)) {
1763 CERROR("count %zd > size %zd\n", count, sizeof(param));
1767 scnprintf(tmp_buf, sizeof(tmp_buf), "%.*s%s", (int)count,
1770 count = strlen(param);
1773 rc = string_to_size(val, param, count);
1775 return rc < 0 ? rc : 0;
1777 EXPORT_SYMBOL(sysfs_memparse);
1779 char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1788 if (!memcmp(s1, s2, l2))
1794 EXPORT_SYMBOL(lprocfs_strnstr);
1797 * Find the string \a name in the input \a buffer, and return a pointer to the
1798 * value immediately following \a name, reducing \a count appropriately.
1799 * If \a name is not found the original \a buffer is returned.
1801 char *lprocfs_find_named_value(const char *buffer, const char *name,
1805 size_t buflen = *count;
1807 /* there is no strnstr() in rhel5 and ubuntu kernels */
1808 val = lprocfs_strnstr(buffer, name, buflen);
1810 return (char *)buffer;
1812 val += strlen(name); /* skip prefix */
1813 while (val < buffer + buflen && isspace(*val)) /* skip separator */
1817 while (val < buffer + buflen && isalnum(*val)) {
1822 return val - *count;
1824 EXPORT_SYMBOL(lprocfs_find_named_value);
1826 int lprocfs_seq_create(struct proc_dir_entry *parent,
1829 const struct file_operations *seq_fops,
1832 struct proc_dir_entry *entry;
1835 /* Disallow secretly (un)writable entries. */
1836 LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
1838 entry = proc_create_data(name, mode, parent, seq_fops, data);
1845 EXPORT_SYMBOL(lprocfs_seq_create);
1847 int lprocfs_obd_seq_create(struct obd_device *obd,
1850 const struct file_operations *seq_fops,
1853 return lprocfs_seq_create(obd->obd_proc_entry, name,
1854 mode, seq_fops, data);
1856 EXPORT_SYMBOL(lprocfs_obd_seq_create);
1858 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
1860 if (value >= OBD_HIST_MAX)
1861 value = OBD_HIST_MAX - 1;
1863 spin_lock(&oh->oh_lock);
1864 oh->oh_buckets[value]++;
1865 spin_unlock(&oh->oh_lock);
1867 EXPORT_SYMBOL(lprocfs_oh_tally);
1869 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
1871 unsigned int val = 0;
1873 if (likely(value != 0))
1874 val = min(fls(value - 1), OBD_HIST_MAX);
1876 lprocfs_oh_tally(oh, val);
1878 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
1880 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
1882 unsigned long ret = 0;
1885 for (i = 0; i < OBD_HIST_MAX; i++)
1886 ret += oh->oh_buckets[i];
1889 EXPORT_SYMBOL(lprocfs_oh_sum);
1891 void lprocfs_oh_clear(struct obd_histogram *oh)
1893 spin_lock(&oh->oh_lock);
1894 memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
1895 spin_unlock(&oh->oh_lock);
1897 EXPORT_SYMBOL(lprocfs_oh_clear);
1899 ssize_t lustre_attr_show(struct kobject *kobj,
1900 struct attribute *attr, char *buf)
1902 struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1904 return a->show ? a->show(kobj, attr, buf) : 0;
1906 EXPORT_SYMBOL_GPL(lustre_attr_show);
1908 ssize_t lustre_attr_store(struct kobject *kobj, struct attribute *attr,
1909 const char *buf, size_t len)
1911 struct lustre_attr *a = container_of(attr, struct lustre_attr, attr);
1913 return a->store ? a->store(kobj, attr, buf, len) : len;
1915 EXPORT_SYMBOL_GPL(lustre_attr_store);
1917 const struct sysfs_ops lustre_sysfs_ops = {
1918 .show = lustre_attr_show,
1919 .store = lustre_attr_store,
1921 EXPORT_SYMBOL_GPL(lustre_sysfs_ops);
1923 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
1925 struct obd_device *obd = data;
1926 struct client_obd *cli = &obd->u.cli;
1928 spin_lock(&cli->cl_loi_list_lock);
1929 seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
1930 spin_unlock(&cli->cl_loi_list_lock);
1933 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
1935 ssize_t lprocfs_obd_max_pages_per_rpc_seq_write(struct file *file,
1936 const char __user *buffer,
1937 size_t count, loff_t *off)
1939 struct seq_file *m = file->private_data;
1940 struct obd_device *obd = m->private;
1941 struct client_obd *cli = &obd->u.cli;
1942 struct obd_import *imp;
1943 struct obd_connect_data *ocd;
1948 if (count > sizeof(kernbuf) - 1)
1951 if (copy_from_user(kernbuf, buffer, count))
1954 kernbuf[count] = '\0';
1956 rc = sysfs_memparse(kernbuf, count, &val, "B");
1960 /* if the max_pages is specified in bytes, convert to pages */
1961 if (val >= ONE_MB_BRW_SIZE)
1964 with_imp_locked(obd, imp, rc) {
1965 ocd = &imp->imp_connect_data;
1966 chunk_mask = ~((1 << (cli->cl_chunkbits - PAGE_SHIFT)) - 1);
1967 /* max_pages_per_rpc must be chunk aligned */
1968 val = (val + ~chunk_mask) & chunk_mask;
1969 if (val == 0 || (ocd->ocd_brw_size != 0 &&
1970 val > ocd->ocd_brw_size >> PAGE_SHIFT)) {
1973 spin_lock(&cli->cl_loi_list_lock);
1974 cli->cl_max_pages_per_rpc = val;
1975 client_adjust_max_dirty(cli);
1976 spin_unlock(&cli->cl_loi_list_lock);
1982 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_write);
1984 ssize_t short_io_bytes_show(struct kobject *kobj, struct attribute *attr,
1987 struct obd_device *obd = container_of(kobj, struct obd_device,
1989 struct client_obd *cli = &obd->u.cli;
1992 spin_lock(&cli->cl_loi_list_lock);
1993 rc = sprintf(buf, "%d\n", cli->cl_max_short_io_bytes);
1994 spin_unlock(&cli->cl_loi_list_lock);
1997 EXPORT_SYMBOL(short_io_bytes_show);
1999 /* Used to catch people who think they're specifying pages. */
2000 #define MIN_SHORT_IO_BYTES 64U
2002 ssize_t short_io_bytes_store(struct kobject *kobj, struct attribute *attr,
2003 const char *buffer, size_t count)
2005 struct obd_device *obd = container_of(kobj, struct obd_device,
2007 struct client_obd *cli = &obd->u.cli;
2011 if (strcmp(buffer, "-1") == 0) {
2012 val = OBD_DEF_SHORT_IO_BYTES;
2014 rc = sysfs_memparse(buffer, count, &val, "B");
2019 if (val && (val < MIN_SHORT_IO_BYTES || val > LNET_MTU))
2020 GOTO(out, rc = -ERANGE);
2024 spin_lock(&cli->cl_loi_list_lock);
2025 cli->cl_max_short_io_bytes = min_t(u64, val, OST_MAX_SHORT_IO_BYTES);
2026 spin_unlock(&cli->cl_loi_list_lock);
2031 EXPORT_SYMBOL(short_io_bytes_store);
2033 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2034 struct root_squash_info *squash, char *name)
2037 char kernbuf[64], *tmp, *errmsg;
2038 unsigned long uid, gid;
2041 if (count >= sizeof(kernbuf)) {
2042 errmsg = "string too long";
2043 GOTO(failed_noprint, rc = -EINVAL);
2045 if (copy_from_user(kernbuf, buffer, count)) {
2046 errmsg = "bad address";
2047 GOTO(failed_noprint, rc = -EFAULT);
2049 kernbuf[count] = '\0';
2051 /* look for uid gid separator */
2052 tmp = strchr(kernbuf, ':');
2054 errmsg = "needs uid:gid format";
2055 GOTO(failed, rc = -EINVAL);
2061 if (kstrtoul(kernbuf, 0, &uid) != 0) {
2063 GOTO(failed, rc = -EINVAL);
2067 if (kstrtoul(tmp, 0, &gid) != 0) {
2069 GOTO(failed, rc = -EINVAL);
2072 squash->rsi_uid = uid;
2073 squash->rsi_gid = gid;
2075 LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2076 name, squash->rsi_uid, squash->rsi_gid);
2084 CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2085 name, kernbuf, errmsg, rc);
2088 CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2092 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2095 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2096 struct root_squash_info *squash, char *name)
2099 char *kernbuf = NULL;
2106 errmsg = "string too long";
2107 GOTO(failed, rc = -EINVAL);
2110 OBD_ALLOC(kernbuf, count + 1);
2112 errmsg = "no memory";
2113 GOTO(failed, rc = -ENOMEM);
2115 if (copy_from_user(kernbuf, buffer, count)) {
2116 errmsg = "bad address";
2117 GOTO(failed, rc = -EFAULT);
2119 kernbuf[count] = '\0';
2121 if (count > 0 && kernbuf[count - 1] == '\n')
2124 if ((len == 4 && strncmp(kernbuf, "NONE", len) == 0) ||
2125 (len == 5 && strncmp(kernbuf, "clear", len) == 0)) {
2126 /* empty string is special case */
2127 spin_lock(&squash->rsi_lock);
2128 if (!list_empty(&squash->rsi_nosquash_nids))
2129 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2130 spin_unlock(&squash->rsi_lock);
2131 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2132 OBD_FREE(kernbuf, count + 1);
2136 if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2137 errmsg = "can't parse";
2138 GOTO(failed, rc = -EINVAL);
2140 LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2142 OBD_FREE(kernbuf, count + 1);
2145 spin_lock(&squash->rsi_lock);
2146 if (!list_empty(&squash->rsi_nosquash_nids))
2147 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2148 list_splice(&tmp, &squash->rsi_nosquash_nids);
2149 spin_unlock(&squash->rsi_lock);
2155 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2156 name, kernbuf, errmsg, rc);
2157 OBD_FREE(kernbuf, count + 1);
2159 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2164 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2166 #endif /* CONFIG_PROC_FS*/