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, 2016, 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>
41 #include <lustre/lustre_idl.h>
45 static int lprocfs_no_percpu_stats = 0;
46 module_param(lprocfs_no_percpu_stats, int, 0644);
47 MODULE_PARM_DESC(lprocfs_no_percpu_stats, "Do not alloc percpu data for lprocfs stats");
49 #define MAX_STRING_SIZE 128
51 int lprocfs_single_release(struct inode *inode, struct file *file)
53 return single_release(inode, file);
55 EXPORT_SYMBOL(lprocfs_single_release);
57 int lprocfs_seq_release(struct inode *inode, struct file *file)
59 return seq_release(inode, file);
61 EXPORT_SYMBOL(lprocfs_seq_release);
63 struct proc_dir_entry *
64 lprocfs_add_simple(struct proc_dir_entry *root, char *name,
65 void *data, const struct file_operations *fops)
67 struct proc_dir_entry *proc;
70 if (root == NULL || name == NULL || fops == NULL)
71 return ERR_PTR(-EINVAL);
77 proc = proc_create_data(name, mode, root, fops, data);
79 CERROR("LprocFS: No memory to create /proc entry %s\n",
81 return ERR_PTR(-ENOMEM);
85 EXPORT_SYMBOL(lprocfs_add_simple);
87 struct proc_dir_entry *lprocfs_add_symlink(const char *name,
88 struct proc_dir_entry *parent, const char *format, ...)
90 struct proc_dir_entry *entry;
94 if (parent == NULL || format == NULL)
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 = { };
120 * \param root [in] The parent proc entry on which new entry will be added.
121 * \param list [in] Array of proc entries to be added.
122 * \param data [in] The argument to be passed when entries read/write routines
123 * are called through /proc file.
125 * \retval 0 on success
129 lprocfs_add_vars(struct proc_dir_entry *root, struct lprocfs_vars *list,
132 if (root == NULL || list == NULL)
135 while (list->name != NULL) {
136 struct proc_dir_entry *proc;
139 if (list->proc_mode != 0000) {
140 mode = list->proc_mode;
141 } else if (list->fops) {
142 if (list->fops->read)
144 if (list->fops->write)
147 proc = proc_create_data(list->name, mode, root,
148 list->fops ?: &lprocfs_generic_fops,
156 EXPORT_SYMBOL(lprocfs_add_vars);
158 #ifndef HAVE_REMOVE_PROC_SUBTREE
159 /* for b=10866, global variable */
160 DECLARE_RWSEM(_lprocfs_lock);
161 EXPORT_SYMBOL(_lprocfs_lock);
163 static void lprocfs_remove_nolock(struct proc_dir_entry **proot)
165 struct proc_dir_entry *root = *proot;
166 struct proc_dir_entry *temp = root;
167 struct proc_dir_entry *rm_entry;
168 struct proc_dir_entry *parent;
171 if (root == NULL || IS_ERR(root))
174 parent = root->parent;
175 LASSERT(parent != NULL);
178 while (temp->subdir != NULL)
184 /* Memory corruption once caused this to fail, and
185 without this LASSERT we would loop here forever. */
186 LASSERTF(strlen(rm_entry->name) == rm_entry->namelen,
187 "0x%p %s/%s len %d\n", rm_entry, temp->name,
188 rm_entry->name, (int)strlen(rm_entry->name));
190 remove_proc_entry(rm_entry->name, temp);
196 int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
198 struct proc_dir_entry *t = NULL;
199 struct proc_dir_entry **p;
202 LASSERT(parent != NULL);
205 down_write(&_lprocfs_lock);
206 /* lookup target name */
207 for (p = &parent->subdir; *p; p = &(*p)->next) {
208 if ((*p)->namelen != len)
210 if (memcmp(name, (*p)->name, len))
217 /* verify it's empty: do not count "num_refs" */
218 for (p = &t->subdir; *p; p = &(*p)->next) {
219 if ((*p)->namelen != strlen("num_refs")) {
223 if (memcmp("num_refs", (*p)->name,
224 strlen("num_refs"))) {
232 lprocfs_remove_nolock(&t);
234 up_write(&_lprocfs_lock);
237 #endif /* !HAVE_REMOVE_PROC_SUBTREE */
239 #ifndef HAVE_PROC_REMOVE
240 void proc_remove(struct proc_dir_entry *de)
242 #ifndef HAVE_REMOVE_PROC_SUBTREE
243 down_write(&_lprocfs_lock); /* search vs remove race */
244 lprocfs_remove_nolock(&de);
245 up_write(&_lprocfs_lock);
248 remove_proc_subtree(de->name, de->parent);
253 void lprocfs_remove(struct proc_dir_entry **rooth)
258 EXPORT_SYMBOL(lprocfs_remove);
260 void lprocfs_remove_proc_entry(const char *name, struct proc_dir_entry *parent)
262 LASSERT(parent != NULL);
263 remove_proc_entry(name, parent);
265 EXPORT_SYMBOL(lprocfs_remove_proc_entry);
267 struct proc_dir_entry *
268 lprocfs_register(const char *name, struct proc_dir_entry *parent,
269 struct lprocfs_vars *list, void *data)
271 struct proc_dir_entry *newchild;
273 newchild = proc_mkdir(name, parent);
274 if (newchild == NULL)
275 return ERR_PTR(-ENOMEM);
278 int rc = lprocfs_add_vars(newchild, list, data);
280 lprocfs_remove(&newchild);
286 EXPORT_SYMBOL(lprocfs_register);
288 /* Generic callbacks */
289 int lprocfs_uint_seq_show(struct seq_file *m, void *data)
291 seq_printf(m, "%u\n", *(unsigned int *)data);
294 EXPORT_SYMBOL(lprocfs_uint_seq_show);
296 int lprocfs_wr_uint(struct file *file, const char __user *buffer,
297 unsigned long count, void *data)
300 char dummy[MAX_STRING_SIZE + 1];
304 if (count >= sizeof(dummy))
310 if (copy_from_user(dummy, buffer, count))
315 tmp = simple_strtoul(dummy, &end, 0);
319 *p = (unsigned int)tmp;
322 EXPORT_SYMBOL(lprocfs_wr_uint);
324 ssize_t lprocfs_uint_seq_write(struct file *file, const char __user *buffer,
325 size_t count, loff_t *off)
327 int *data = ((struct seq_file *)file->private_data)->private;
331 rc = lprocfs_str_to_s64(buffer, count, &val);
335 return lprocfs_wr_uint(file, buffer, count, data);
337 EXPORT_SYMBOL(lprocfs_uint_seq_write);
339 int lprocfs_u64_seq_show(struct seq_file *m, void *data)
341 LASSERT(data != NULL);
342 seq_printf(m, "%llu\n", *(__u64 *)data);
345 EXPORT_SYMBOL(lprocfs_u64_seq_show);
347 int lprocfs_atomic_seq_show(struct seq_file *m, void *data)
349 atomic_t *atom = data;
350 LASSERT(atom != NULL);
351 seq_printf(m, "%d\n", atomic_read(atom));
354 EXPORT_SYMBOL(lprocfs_atomic_seq_show);
357 lprocfs_atomic_seq_write(struct file *file, const char __user *buffer,
358 size_t count, loff_t *off)
360 atomic_t *atm = ((struct seq_file *)file->private_data)->private;
364 rc = lprocfs_str_to_s64(buffer, count, &val);
368 if (val <= 0 || val > INT_MAX)
371 atomic_set(atm, val);
374 EXPORT_SYMBOL(lprocfs_atomic_seq_write);
376 int lprocfs_uuid_seq_show(struct seq_file *m, void *data)
378 struct obd_device *obd = data;
380 LASSERT(obd != NULL);
381 seq_printf(m, "%s\n", obd->obd_uuid.uuid);
384 EXPORT_SYMBOL(lprocfs_uuid_seq_show);
386 int lprocfs_name_seq_show(struct seq_file *m, void *data)
388 struct obd_device *dev = data;
390 LASSERT(dev != NULL);
391 seq_printf(m, "%s\n", dev->obd_name);
394 EXPORT_SYMBOL(lprocfs_name_seq_show);
396 int lprocfs_blksize_seq_show(struct seq_file *m, void *data)
398 struct obd_device *obd = data;
399 struct obd_statfs osfs;
400 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
401 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
404 seq_printf(m, "%u\n", osfs.os_bsize);
407 EXPORT_SYMBOL(lprocfs_blksize_seq_show);
409 int lprocfs_kbytestotal_seq_show(struct seq_file *m, void *data)
411 struct obd_device *obd = data;
412 struct obd_statfs osfs;
413 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
414 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
417 __u32 blk_size = osfs.os_bsize >> 10;
418 __u64 result = osfs.os_blocks;
420 while (blk_size >>= 1)
423 seq_printf(m, "%llu\n", result);
427 EXPORT_SYMBOL(lprocfs_kbytestotal_seq_show);
429 int lprocfs_kbytesfree_seq_show(struct seq_file *m, void *data)
431 struct obd_device *obd = data;
432 struct obd_statfs osfs;
433 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
434 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
437 __u32 blk_size = osfs.os_bsize >> 10;
438 __u64 result = osfs.os_bfree;
440 while (blk_size >>= 1)
443 seq_printf(m, "%llu\n", result);
447 EXPORT_SYMBOL(lprocfs_kbytesfree_seq_show);
449 int lprocfs_kbytesavail_seq_show(struct seq_file *m, void *data)
451 struct obd_device *obd = data;
452 struct obd_statfs osfs;
453 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
454 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
457 __u32 blk_size = osfs.os_bsize >> 10;
458 __u64 result = osfs.os_bavail;
460 while (blk_size >>= 1)
463 seq_printf(m, "%llu\n", result);
467 EXPORT_SYMBOL(lprocfs_kbytesavail_seq_show);
469 int lprocfs_filestotal_seq_show(struct seq_file *m, void *data)
471 struct obd_device *obd = data;
472 struct obd_statfs osfs;
473 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
474 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
477 seq_printf(m, "%llu\n", osfs.os_files);
480 EXPORT_SYMBOL(lprocfs_filestotal_seq_show);
482 int lprocfs_filesfree_seq_show(struct seq_file *m, void *data)
484 struct obd_device *obd = data;
485 struct obd_statfs osfs;
486 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
487 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
490 seq_printf(m, "%llu\n", osfs.os_ffree);
493 EXPORT_SYMBOL(lprocfs_filesfree_seq_show);
495 int lprocfs_server_uuid_seq_show(struct seq_file *m, void *data)
497 struct obd_device *obd = data;
498 struct obd_import *imp;
499 char *imp_state_name = NULL;
502 LASSERT(obd != NULL);
503 LPROCFS_CLIMP_CHECK(obd);
504 imp = obd->u.cli.cl_import;
505 imp_state_name = ptlrpc_import_state_name(imp->imp_state);
506 seq_printf(m, "%s\t%s%s\n", obd2cli_tgt(obd), imp_state_name,
507 imp->imp_deactive ? "\tDEACTIVATED" : "");
509 LPROCFS_CLIMP_EXIT(obd);
512 EXPORT_SYMBOL(lprocfs_server_uuid_seq_show);
514 int lprocfs_conn_uuid_seq_show(struct seq_file *m, void *data)
516 struct obd_device *obd = data;
517 struct ptlrpc_connection *conn;
520 LASSERT(obd != NULL);
522 LPROCFS_CLIMP_CHECK(obd);
523 conn = obd->u.cli.cl_import->imp_connection;
524 if (conn && obd->u.cli.cl_import)
525 seq_printf(m, "%s\n", conn->c_remote_uuid.uuid);
527 seq_printf(m, "%s\n", "<none>");
529 LPROCFS_CLIMP_EXIT(obd);
532 EXPORT_SYMBOL(lprocfs_conn_uuid_seq_show);
534 /** add up per-cpu counters */
537 * Lock statistics structure for access, possibly only on this CPU.
539 * The statistics struct may be allocated with per-CPU structures for
540 * efficient concurrent update (usually only on server-wide stats), or
541 * as a single global struct (e.g. for per-client or per-job statistics),
542 * so the required locking depends on the type of structure allocated.
544 * For per-CPU statistics, pin the thread to the current cpuid so that
545 * will only access the statistics for that CPU. If the stats structure
546 * for the current CPU has not been allocated (or previously freed),
547 * allocate it now. The per-CPU statistics do not need locking since
548 * the thread is pinned to the CPU during update.
550 * For global statistics, lock the stats structure to prevent concurrent update.
552 * \param[in] stats statistics structure to lock
553 * \param[in] opc type of operation:
554 * LPROCFS_GET_SMP_ID: "lock" and return current CPU index
555 * for incrementing statistics for that CPU
556 * LPROCFS_GET_NUM_CPU: "lock" and return number of used
557 * CPU indices to iterate over all indices
558 * \param[out] flags CPU interrupt saved state for IRQ-safe locking
560 * \retval cpuid of current thread or number of allocated structs
561 * \retval negative on error (only for opc LPROCFS_GET_SMP_ID + per-CPU stats)
563 int lprocfs_stats_lock(struct lprocfs_stats *stats,
564 enum lprocfs_stats_lock_ops opc,
565 unsigned long *flags)
567 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
568 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
569 spin_lock_irqsave(&stats->ls_lock, *flags);
571 spin_lock(&stats->ls_lock);
572 return opc == LPROCFS_GET_NUM_CPU ? 1 : 0;
576 case LPROCFS_GET_SMP_ID: {
577 unsigned int cpuid = get_cpu();
579 if (unlikely(!stats->ls_percpu[cpuid])) {
580 int rc = lprocfs_stats_alloc_one(stats, cpuid);
589 case LPROCFS_GET_NUM_CPU:
590 return stats->ls_biggest_alloc_num;
597 * Unlock statistics structure after access.
599 * Unlock the lock acquired via lprocfs_stats_lock() for global statistics,
600 * or unpin this thread from the current cpuid for per-CPU statistics.
602 * This function must be called using the same arguments as used when calling
603 * lprocfs_stats_lock() so that the correct operation can be performed.
605 * \param[in] stats statistics structure to unlock
606 * \param[in] opc type of operation (current cpuid or number of structs)
607 * \param[in] flags CPU interrupt saved state for IRQ-safe locking
609 void lprocfs_stats_unlock(struct lprocfs_stats *stats,
610 enum lprocfs_stats_lock_ops opc,
611 unsigned long *flags)
613 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
614 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
615 spin_unlock_irqrestore(&stats->ls_lock, *flags);
617 spin_unlock(&stats->ls_lock);
618 } else if (opc == LPROCFS_GET_SMP_ID) {
623 /** add up per-cpu counters */
624 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
625 struct lprocfs_counter *cnt)
627 unsigned int num_entry;
628 struct lprocfs_counter *percpu_cntr;
630 unsigned long flags = 0;
632 memset(cnt, 0, sizeof(*cnt));
635 /* set count to 1 to avoid divide-by-zero errs in callers */
640 cnt->lc_min = LC_MIN_INIT;
642 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
644 for (i = 0; i < num_entry; i++) {
645 if (stats->ls_percpu[i] == NULL)
647 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
649 cnt->lc_count += percpu_cntr->lc_count;
650 cnt->lc_sum += percpu_cntr->lc_sum;
651 if (percpu_cntr->lc_min < cnt->lc_min)
652 cnt->lc_min = percpu_cntr->lc_min;
653 if (percpu_cntr->lc_max > cnt->lc_max)
654 cnt->lc_max = percpu_cntr->lc_max;
655 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
658 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
662 * Append a space separated list of current set flags to str.
664 #define flag2str(flag) \
666 if (imp->imp_##flag) { \
667 seq_printf(m, "%s" #flag, first ? "" : ", "); \
671 static void obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
675 if (imp->imp_obd->obd_no_recov) {
676 seq_printf(m, "no_recov");
682 flag2str(replayable);
683 flag2str(delayed_recovery);
684 flag2str(no_lock_replay);
685 flag2str(vbr_failed);
687 flag2str(resend_replay);
688 flag2str(no_pinger_recover);
689 flag2str(need_mne_swab);
690 flag2str(connect_tried);
694 static const char *obd_connect_names[] = {
709 "join_file(obsolete)",
713 "remote_client_by_force",
722 "mds_mds_connection",
725 "alt_checksum_algorithm",
765 static void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags,
766 __u64 flags2, const char *sep)
772 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
774 seq_printf(m, "%s%s",
775 first ? "" : sep, obd_connect_names[i]);
780 if (flags & ~(mask - 1)) {
781 seq_printf(m, "%sunknown_%#llx",
782 first ? "" : sep, flags & ~(mask - 1));
786 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
789 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
791 seq_printf(m, "%s%s",
792 first ? "" : sep, obd_connect_names[i]);
797 if (flags2 & ~(mask - 1)) {
798 seq_printf(m, "%sunknown2_%#llx",
799 first ? "" : sep, flags2 & ~(mask - 1));
804 int obd_connect_flags2str(char *page, int count, __u64 flags, __u64 flags2,
810 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
812 ret += snprintf(page + ret, count - ret, "%s%s",
813 ret ? sep : "", obd_connect_names[i]);
816 if (flags & ~(mask - 1))
817 ret += snprintf(page + ret, count - ret,
819 ret ? sep : "", flags & ~(mask - 1));
821 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
824 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
826 ret += snprintf(page + ret, count - ret, "%s%s",
827 ret ? sep : "", obd_connect_names[i]);
830 if (flags2 & ~(mask - 1))
831 ret += snprintf(page + ret, count - ret,
833 ret ? sep : "", flags2 & ~(mask - 1));
837 EXPORT_SYMBOL(obd_connect_flags2str);
839 static void obd_connect_data_seqprint(struct seq_file *m,
840 struct obd_connect_data *ocd)
844 LASSERT(ocd != NULL);
845 flags = ocd->ocd_connect_flags;
847 seq_printf(m, " connect_data:\n"
850 ocd->ocd_connect_flags,
852 if (flags & OBD_CONNECT_VERSION)
853 seq_printf(m, " target_version: %u.%u.%u.%u\n",
854 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
855 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
856 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
857 OBD_OCD_VERSION_FIX(ocd->ocd_version));
858 if (flags & OBD_CONNECT_MDS)
859 seq_printf(m, " mdt_index: %d\n", ocd->ocd_group);
860 if (flags & OBD_CONNECT_GRANT)
861 seq_printf(m, " initial_grant: %d\n", ocd->ocd_grant);
862 if (flags & OBD_CONNECT_INDEX)
863 seq_printf(m, " target_index: %u\n", ocd->ocd_index);
864 if (flags & OBD_CONNECT_BRW_SIZE)
865 seq_printf(m, " max_brw_size: %d\n", ocd->ocd_brw_size);
866 if (flags & OBD_CONNECT_IBITS)
867 seq_printf(m, " ibits_known: %#llx\n",
868 ocd->ocd_ibits_known);
869 if (flags & OBD_CONNECT_GRANT_PARAM)
870 seq_printf(m, " grant_block_size: %d\n"
871 " grant_inode_size: %d\n"
872 " grant_max_extent_size: %d\n"
873 " grant_extent_tax: %d\n",
874 1 << ocd->ocd_grant_blkbits,
875 1 << ocd->ocd_grant_inobits,
876 ocd->ocd_grant_max_blks << ocd->ocd_grant_blkbits,
877 ocd->ocd_grant_tax_kb << 10);
878 if (flags & OBD_CONNECT_TRANSNO)
879 seq_printf(m, " first_transno: %#llx\n",
881 if (flags & OBD_CONNECT_CKSUM)
882 seq_printf(m, " cksum_types: %#x\n",
883 ocd->ocd_cksum_types);
884 if (flags & OBD_CONNECT_MAX_EASIZE)
885 seq_printf(m, " max_easize: %d\n", ocd->ocd_max_easize);
886 if (flags & OBD_CONNECT_MAXBYTES)
887 seq_printf(m, " max_object_bytes: %llu\n",
889 if (flags & OBD_CONNECT_MULTIMODRPCS)
890 seq_printf(m, " max_mod_rpcs: %hu\n",
891 ocd->ocd_maxmodrpcs);
894 int lprocfs_import_seq_show(struct seq_file *m, void *data)
896 char nidstr[LNET_NIDSTR_SIZE];
897 struct lprocfs_counter ret;
898 struct lprocfs_counter_header *header;
899 struct obd_device *obd = (struct obd_device *)data;
900 struct obd_import *imp;
901 struct obd_import_conn *conn;
902 struct obd_connect_data *ocd;
907 LASSERT(obd != NULL);
908 LPROCFS_CLIMP_CHECK(obd);
909 imp = obd->u.cli.cl_import;
910 ocd = &imp->imp_connect_data;
912 seq_printf(m, "import:\n"
916 " connect_flags: [ ",
919 ptlrpc_import_state_name(imp->imp_state));
920 obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
921 imp->imp_connect_data.ocd_connect_flags2,
923 seq_printf(m, " ]\n");
924 obd_connect_data_seqprint(m, ocd);
925 seq_printf(m, " import_flags: [ ");
926 obd_import_flags2str(imp, m);
930 " failover_nids: [ ");
931 spin_lock(&imp->imp_lock);
933 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
934 libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
935 nidstr, sizeof(nidstr));
936 seq_printf(m, "%s%s", j ? ", " : "", nidstr);
939 if (imp->imp_connection != NULL)
940 libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
941 nidstr, sizeof(nidstr));
943 strncpy(nidstr, "<none>", sizeof(nidstr));
945 " current_connection: %s\n"
946 " connection_attempts: %u\n"
948 " in-progress_invalidations: %u\n",
952 atomic_read(&imp->imp_inval_count));
953 spin_unlock(&imp->imp_lock);
955 if (obd->obd_svc_stats == NULL)
958 header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
959 lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
960 if (ret.lc_count != 0) {
961 /* first argument to do_div MUST be __u64 */
962 __u64 sum = ret.lc_sum;
963 do_div(sum, ret.lc_count);
967 seq_printf(m, " rpcs:\n"
969 " unregistering: %u\n"
971 " avg_waittime: %llu %s\n",
972 atomic_read(&imp->imp_inflight),
973 atomic_read(&imp->imp_unregistering),
974 atomic_read(&imp->imp_timeouts),
975 ret.lc_sum, header->lc_units);
978 for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
979 if (imp->imp_at.iat_portal[j] == 0)
981 k = max_t(unsigned int, k,
982 at_get(&imp->imp_at.iat_service_estimate[j]));
984 seq_printf(m, " service_estimates:\n"
985 " services: %u sec\n"
986 " network: %u sec\n",
988 at_get(&imp->imp_at.iat_net_latency));
990 seq_printf(m, " transactions:\n"
991 " last_replay: %llu\n"
992 " peer_committed: %llu\n"
993 " last_checked: %llu\n",
994 imp->imp_last_replay_transno,
995 imp->imp_peer_committed_transno,
996 imp->imp_last_transno_checked);
999 for (rw = 0; rw <= 1; rw++) {
1000 lprocfs_stats_collect(obd->obd_svc_stats,
1001 PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
1003 if (ret.lc_sum > 0 && ret.lc_count > 0) {
1004 /* first argument to do_div MUST be __u64 */
1005 __u64 sum = ret.lc_sum;
1006 do_div(sum, ret.lc_count);
1008 seq_printf(m, " %s_data_averages:\n"
1009 " bytes_per_rpc: %llu\n",
1010 rw ? "write" : "read",
1013 k = (int)ret.lc_sum;
1014 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
1015 header = &obd->obd_svc_stats->ls_cnt_header[j];
1016 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
1017 if (ret.lc_sum > 0 && ret.lc_count != 0) {
1018 /* first argument to do_div MUST be __u64 */
1019 __u64 sum = ret.lc_sum;
1020 do_div(sum, ret.lc_count);
1022 seq_printf(m, " %s_per_rpc: %llu\n",
1023 header->lc_units, ret.lc_sum);
1024 j = (int)ret.lc_sum;
1026 seq_printf(m, " MB_per_sec: %u.%.02u\n",
1027 k / j, (100 * k / j) % 100);
1032 LPROCFS_CLIMP_EXIT(obd);
1035 EXPORT_SYMBOL(lprocfs_import_seq_show);
1037 int lprocfs_state_seq_show(struct seq_file *m, void *data)
1039 struct obd_device *obd = (struct obd_device *)data;
1040 struct obd_import *imp;
1043 LASSERT(obd != NULL);
1044 LPROCFS_CLIMP_CHECK(obd);
1045 imp = obd->u.cli.cl_import;
1047 seq_printf(m, "current_state: %s\n",
1048 ptlrpc_import_state_name(imp->imp_state));
1049 seq_printf(m, "state_history:\n");
1050 k = imp->imp_state_hist_idx;
1051 for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
1052 struct import_state_hist *ish =
1053 &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
1054 if (ish->ish_state == 0)
1056 seq_printf(m, " - [ "CFS_TIME_T", %s ]\n",
1058 ptlrpc_import_state_name(ish->ish_state));
1061 LPROCFS_CLIMP_EXIT(obd);
1064 EXPORT_SYMBOL(lprocfs_state_seq_show);
1066 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
1069 for (i = 0; i < AT_BINS; i++)
1070 seq_printf(m, "%3u ", at->at_hist[i]);
1071 seq_printf(m, "\n");
1074 EXPORT_SYMBOL(lprocfs_at_hist_helper);
1076 /* See also ptlrpc_lprocfs_timeouts_show_seq */
1077 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
1079 struct obd_device *obd = (struct obd_device *)data;
1080 struct obd_import *imp;
1081 unsigned int cur, worst;
1086 LASSERT(obd != NULL);
1087 LPROCFS_CLIMP_CHECK(obd);
1088 imp = obd->u.cli.cl_import;
1090 now = cfs_time_current_sec();
1092 /* Some network health info for kicks */
1093 s2dhms(&ts, now - imp->imp_last_reply_time);
1094 seq_printf(m, "%-10s : %ld, "DHMS_FMT" ago\n",
1095 "last reply", imp->imp_last_reply_time, DHMS_VARS(&ts));
1097 cur = at_get(&imp->imp_at.iat_net_latency);
1098 worst = imp->imp_at.iat_net_latency.at_worst_ever;
1099 worstt = imp->imp_at.iat_net_latency.at_worst_time;
1100 s2dhms(&ts, now - worstt);
1101 seq_printf(m, "%-10s : cur %3u worst %3u (at %ld, "DHMS_FMT" ago) ",
1102 "network", cur, worst, worstt, DHMS_VARS(&ts));
1103 lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
1105 for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1106 if (imp->imp_at.iat_portal[i] == 0)
1108 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
1109 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
1110 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
1111 s2dhms(&ts, now - worstt);
1112 seq_printf(m, "portal %-2d : cur %3u worst %3u (at %ld, "
1113 DHMS_FMT" ago) ", imp->imp_at.iat_portal[i],
1114 cur, worst, worstt, DHMS_VARS(&ts));
1115 lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
1118 LPROCFS_CLIMP_EXIT(obd);
1121 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
1123 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
1125 struct obd_device *obd = data;
1129 LPROCFS_CLIMP_CHECK(obd);
1130 flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
1131 flags2 = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags2;
1132 seq_printf(m, "flags=%#llx\n", flags);
1133 seq_printf(m, "flags2=%#llx\n", flags2);
1134 obd_connect_seq_flags2str(m, flags, flags2, "\n");
1135 seq_printf(m, "\n");
1136 LPROCFS_CLIMP_EXIT(obd);
1139 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
1142 lprocfs_obd_setup(struct obd_device *obd)
1146 LASSERT(obd != NULL);
1147 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1148 LASSERT(obd->obd_type->typ_procroot != NULL);
1150 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1151 obd->obd_type->typ_procroot,
1152 obd->obd_vars, obd);
1153 if (IS_ERR(obd->obd_proc_entry)) {
1154 rc = PTR_ERR(obd->obd_proc_entry);
1155 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1156 obd->obd_proc_entry = NULL;
1160 EXPORT_SYMBOL(lprocfs_obd_setup);
1162 int lprocfs_obd_cleanup(struct obd_device *obd)
1166 if (obd->obd_proc_exports_entry) {
1167 /* Should be no exports left */
1168 lprocfs_remove(&obd->obd_proc_exports_entry);
1169 obd->obd_proc_exports_entry = NULL;
1171 if (obd->obd_proc_entry) {
1172 lprocfs_remove(&obd->obd_proc_entry);
1173 obd->obd_proc_entry = NULL;
1177 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1179 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1181 struct lprocfs_counter *cntr;
1182 unsigned int percpusize;
1184 unsigned long flags = 0;
1187 LASSERT(stats->ls_percpu[cpuid] == NULL);
1188 LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1190 percpusize = lprocfs_stats_counter_size(stats);
1191 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1192 if (stats->ls_percpu[cpuid] != NULL) {
1194 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1195 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1196 spin_lock_irqsave(&stats->ls_lock, flags);
1198 spin_lock(&stats->ls_lock);
1199 if (stats->ls_biggest_alloc_num <= cpuid)
1200 stats->ls_biggest_alloc_num = cpuid + 1;
1201 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1202 spin_unlock_irqrestore(&stats->ls_lock, flags);
1204 spin_unlock(&stats->ls_lock);
1207 /* initialize the ls_percpu[cpuid] non-zero counter */
1208 for (i = 0; i < stats->ls_num; ++i) {
1209 cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1210 cntr->lc_min = LC_MIN_INIT;
1216 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1217 enum lprocfs_stats_flags flags)
1219 struct lprocfs_stats *stats;
1220 unsigned int num_entry;
1221 unsigned int percpusize = 0;
1227 if (lprocfs_no_percpu_stats != 0)
1228 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1230 if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1233 num_entry = num_possible_cpus();
1235 /* alloc percpu pointers for all possible cpu slots */
1236 LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1240 stats->ls_num = num;
1241 stats->ls_flags = flags;
1242 spin_lock_init(&stats->ls_lock);
1244 /* alloc num of counter headers */
1245 LIBCFS_ALLOC(stats->ls_cnt_header,
1246 stats->ls_num * sizeof(struct lprocfs_counter_header));
1247 if (stats->ls_cnt_header == NULL)
1250 if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1251 /* contains only one set counters */
1252 percpusize = lprocfs_stats_counter_size(stats);
1253 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1254 if (stats->ls_percpu[0] == NULL)
1256 stats->ls_biggest_alloc_num = 1;
1257 } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1258 /* alloc all percpu data, currently only obd_memory use this */
1259 for (i = 0; i < num_entry; ++i)
1260 if (lprocfs_stats_alloc_one(stats, i) < 0)
1267 lprocfs_free_stats(&stats);
1270 EXPORT_SYMBOL(lprocfs_alloc_stats);
1272 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1274 struct lprocfs_stats *stats = *statsh;
1275 unsigned int num_entry;
1276 unsigned int percpusize;
1279 if (stats == NULL || stats->ls_num == 0)
1283 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1286 num_entry = num_possible_cpus();
1288 percpusize = lprocfs_stats_counter_size(stats);
1289 for (i = 0; i < num_entry; i++)
1290 if (stats->ls_percpu[i] != NULL)
1291 LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1292 if (stats->ls_cnt_header != NULL)
1293 LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
1294 sizeof(struct lprocfs_counter_header));
1295 LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1297 EXPORT_SYMBOL(lprocfs_free_stats);
1299 u64 lprocfs_stats_collector(struct lprocfs_stats *stats, int idx,
1300 enum lprocfs_fields_flags field)
1302 unsigned long flags = 0;
1303 unsigned int num_cpu;
1309 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1310 for (i = 0; i < num_cpu; i++) {
1311 struct lprocfs_counter *cntr;
1313 if (!stats->ls_percpu[i])
1316 cntr = lprocfs_stats_counter_get(stats, i, idx);
1317 ret += lprocfs_read_helper(cntr, &stats->ls_cnt_header[idx],
1318 stats->ls_flags, field);
1320 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1323 EXPORT_SYMBOL(lprocfs_stats_collector);
1325 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1327 struct lprocfs_counter *percpu_cntr;
1330 unsigned int num_entry;
1331 unsigned long flags = 0;
1333 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1335 for (i = 0; i < num_entry; i++) {
1336 if (stats->ls_percpu[i] == NULL)
1338 for (j = 0; j < stats->ls_num; j++) {
1339 percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1340 percpu_cntr->lc_count = 0;
1341 percpu_cntr->lc_min = LC_MIN_INIT;
1342 percpu_cntr->lc_max = 0;
1343 percpu_cntr->lc_sumsquare = 0;
1344 percpu_cntr->lc_sum = 0;
1345 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1346 percpu_cntr->lc_sum_irq = 0;
1350 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1352 EXPORT_SYMBOL(lprocfs_clear_stats);
1354 static ssize_t lprocfs_stats_seq_write(struct file *file,
1355 const char __user *buf,
1356 size_t len, loff_t *off)
1358 struct seq_file *seq = file->private_data;
1359 struct lprocfs_stats *stats = seq->private;
1361 lprocfs_clear_stats(stats);
1366 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1368 struct lprocfs_stats *stats = p->private;
1370 return (*pos < stats->ls_num) ? pos : NULL;
1373 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1377 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1381 return lprocfs_stats_seq_start(p, pos);
1384 /* seq file export of one lprocfs counter */
1385 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1387 struct lprocfs_stats *stats = p->private;
1388 struct lprocfs_counter_header *hdr;
1389 struct lprocfs_counter ctr;
1390 int idx = *(loff_t *)v;
1395 do_gettimeofday(&now);
1396 seq_printf(p, "%-25s %lu.%lu secs.usecs\n",
1397 "snapshot_time", now.tv_sec, now.tv_usec);
1400 hdr = &stats->ls_cnt_header[idx];
1401 lprocfs_stats_collect(stats, idx, &ctr);
1403 if (ctr.lc_count == 0)
1406 seq_printf(p, "%-25s %lld samples [%s]", hdr->lc_name,
1407 ctr.lc_count, hdr->lc_units);
1409 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1410 seq_printf(p, " %lld %lld %lld",
1411 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1412 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1413 seq_printf(p, " %llu", ctr.lc_sumsquare);
1419 static const struct seq_operations lprocfs_stats_seq_sops = {
1420 .start = lprocfs_stats_seq_start,
1421 .stop = lprocfs_stats_seq_stop,
1422 .next = lprocfs_stats_seq_next,
1423 .show = lprocfs_stats_seq_show,
1426 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1428 struct seq_file *seq;
1431 rc = LPROCFS_ENTRY_CHECK(inode);
1435 rc = seq_open(file, &lprocfs_stats_seq_sops);
1438 seq = file->private_data;
1439 seq->private = PDE_DATA(inode);
1443 static const struct file_operations lprocfs_stats_seq_fops = {
1444 .owner = THIS_MODULE,
1445 .open = lprocfs_stats_seq_open,
1447 .write = lprocfs_stats_seq_write,
1448 .llseek = seq_lseek,
1449 .release = lprocfs_seq_release,
1452 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1453 struct lprocfs_stats *stats)
1455 struct proc_dir_entry *entry;
1456 LASSERT(root != NULL);
1458 entry = proc_create_data(name, 0644, root,
1459 &lprocfs_stats_seq_fops, stats);
1464 EXPORT_SYMBOL(lprocfs_register_stats);
1466 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1467 unsigned conf, const char *name, const char *units)
1469 struct lprocfs_counter_header *header;
1470 struct lprocfs_counter *percpu_cntr;
1471 unsigned long flags = 0;
1473 unsigned int num_cpu;
1475 LASSERT(stats != NULL);
1477 header = &stats->ls_cnt_header[index];
1478 LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1479 index, name, units);
1481 header->lc_config = conf;
1482 header->lc_name = name;
1483 header->lc_units = units;
1485 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1486 for (i = 0; i < num_cpu; ++i) {
1487 if (stats->ls_percpu[i] == NULL)
1489 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1490 percpu_cntr->lc_count = 0;
1491 percpu_cntr->lc_min = LC_MIN_INIT;
1492 percpu_cntr->lc_max = 0;
1493 percpu_cntr->lc_sumsquare = 0;
1494 percpu_cntr->lc_sum = 0;
1495 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1496 percpu_cntr->lc_sum_irq = 0;
1498 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1500 EXPORT_SYMBOL(lprocfs_counter_init);
1502 /* Note that we only init md counters for ops whose offset is less
1503 * than NUM_MD_STATS. This is explained in a comment in the definition
1504 * of struct md_ops. */
1505 #define LPROCFS_MD_OP_INIT(base, stats, op) \
1507 unsigned int _idx = base + MD_COUNTER_OFFSET(op); \
1509 if (MD_COUNTER_OFFSET(op) < NUM_MD_STATS) { \
1510 LASSERT(_idx < stats->ls_num); \
1511 lprocfs_counter_init(stats, _idx, 0, #op, "reqs"); \
1515 void lprocfs_init_mps_stats(int num_private_stats, struct lprocfs_stats *stats)
1517 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_root);
1518 LPROCFS_MD_OP_INIT(num_private_stats, stats, null_inode);
1519 LPROCFS_MD_OP_INIT(num_private_stats, stats, close);
1520 LPROCFS_MD_OP_INIT(num_private_stats, stats, create);
1521 LPROCFS_MD_OP_INIT(num_private_stats, stats, enqueue);
1522 LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr);
1523 LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr_name);
1524 LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_lock);
1525 LPROCFS_MD_OP_INIT(num_private_stats, stats, link);
1526 LPROCFS_MD_OP_INIT(num_private_stats, stats, rename);
1527 LPROCFS_MD_OP_INIT(num_private_stats, stats, setattr);
1528 LPROCFS_MD_OP_INIT(num_private_stats, stats, fsync);
1529 LPROCFS_MD_OP_INIT(num_private_stats, stats, read_page);
1530 LPROCFS_MD_OP_INIT(num_private_stats, stats, unlink);
1531 LPROCFS_MD_OP_INIT(num_private_stats, stats, setxattr);
1532 LPROCFS_MD_OP_INIT(num_private_stats, stats, getxattr);
1533 LPROCFS_MD_OP_INIT(num_private_stats, stats, init_ea_size);
1534 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_lustre_md);
1535 LPROCFS_MD_OP_INIT(num_private_stats, stats, free_lustre_md);
1536 LPROCFS_MD_OP_INIT(num_private_stats, stats, merge_attr);
1537 LPROCFS_MD_OP_INIT(num_private_stats, stats, set_open_replay_data);
1538 LPROCFS_MD_OP_INIT(num_private_stats, stats, clear_open_replay_data);
1539 LPROCFS_MD_OP_INIT(num_private_stats, stats, set_lock_data);
1540 LPROCFS_MD_OP_INIT(num_private_stats, stats, lock_match);
1541 LPROCFS_MD_OP_INIT(num_private_stats, stats, cancel_unused);
1542 LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_getattr_async);
1543 LPROCFS_MD_OP_INIT(num_private_stats, stats, revalidate_lock);
1546 int lprocfs_alloc_md_stats(struct obd_device *obd,
1547 unsigned int num_private_stats)
1549 struct lprocfs_stats *stats;
1550 unsigned int num_stats;
1553 CLASSERT(offsetof(struct md_ops, MD_STATS_FIRST_OP) == 0);
1554 CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_FIRST_OP) == 0);
1555 CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_LAST_OP) > 0);
1557 /* TODO Ensure that this function is only used where
1558 * appropriate by adding an assertion to the effect that
1559 * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1560 * because mdt_procfs_init() uses this function to allocate
1561 * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1562 * mdt layer does not use the md_ops interface. This is
1563 * confusing and a waste of memory. See LU-2484.
1565 LASSERT(obd->obd_proc_entry != NULL);
1566 LASSERT(obd->obd_md_stats == NULL);
1567 LASSERT(obd->obd_md_cntr_base == 0);
1569 num_stats = NUM_MD_STATS + num_private_stats;
1570 stats = lprocfs_alloc_stats(num_stats, 0);
1574 lprocfs_init_mps_stats(num_private_stats, stats);
1576 for (i = num_private_stats; i < num_stats; i++) {
1577 if (stats->ls_cnt_header[i].lc_name == NULL) {
1578 CERROR("Missing md_stat initializer md_op "
1579 "operation at offset %d. Aborting.\n",
1580 i - num_private_stats);
1585 rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1587 lprocfs_free_stats(&stats);
1589 obd->obd_md_stats = stats;
1590 obd->obd_md_cntr_base = num_private_stats;
1595 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1597 void lprocfs_free_md_stats(struct obd_device *obd)
1599 struct lprocfs_stats *stats = obd->obd_md_stats;
1601 if (stats != NULL) {
1602 obd->obd_md_stats = NULL;
1603 obd->obd_md_cntr_base = 0;
1604 lprocfs_free_stats(&stats);
1607 EXPORT_SYMBOL(lprocfs_free_md_stats);
1609 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1611 lprocfs_counter_init(ldlm_stats,
1612 LDLM_ENQUEUE - LDLM_FIRST_OPC,
1613 0, "ldlm_enqueue", "reqs");
1614 lprocfs_counter_init(ldlm_stats,
1615 LDLM_CONVERT - LDLM_FIRST_OPC,
1616 0, "ldlm_convert", "reqs");
1617 lprocfs_counter_init(ldlm_stats,
1618 LDLM_CANCEL - LDLM_FIRST_OPC,
1619 0, "ldlm_cancel", "reqs");
1620 lprocfs_counter_init(ldlm_stats,
1621 LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1622 0, "ldlm_bl_callback", "reqs");
1623 lprocfs_counter_init(ldlm_stats,
1624 LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1625 0, "ldlm_cp_callback", "reqs");
1626 lprocfs_counter_init(ldlm_stats,
1627 LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1628 0, "ldlm_gl_callback", "reqs");
1630 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1632 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1633 struct lprocfs_counter_header *header,
1634 enum lprocfs_stats_flags flags,
1635 enum lprocfs_fields_flags field)
1639 if (lc == NULL || header == NULL)
1643 case LPROCFS_FIELDS_FLAGS_CONFIG:
1644 ret = header->lc_config;
1646 case LPROCFS_FIELDS_FLAGS_SUM:
1648 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1649 ret += lc->lc_sum_irq;
1651 case LPROCFS_FIELDS_FLAGS_MIN:
1654 case LPROCFS_FIELDS_FLAGS_MAX:
1657 case LPROCFS_FIELDS_FLAGS_AVG:
1658 ret = (lc->lc_max - lc->lc_min) / 2;
1660 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1661 ret = lc->lc_sumsquare;
1663 case LPROCFS_FIELDS_FLAGS_COUNT:
1671 EXPORT_SYMBOL(lprocfs_read_helper);
1673 int lprocfs_read_frac_helper(char *buffer, unsigned long count, long val,
1676 long decimal_val, frac_val;
1682 decimal_val = val / mult;
1683 prtn = snprintf(buffer, count, "%ld", decimal_val);
1684 frac_val = val % mult;
1686 if (prtn < (count - 4) && frac_val > 0) {
1688 int i, temp_mult = 1, frac_bits = 0;
1690 temp_frac = frac_val * 10;
1691 buffer[prtn++] = '.';
1692 while (frac_bits < 2 && (temp_frac / mult) < 1 ) {
1693 /* only reserved 2 bits fraction */
1694 buffer[prtn++] ='0';
1699 * Need to think these cases :
1700 * 1. #echo x.00 > /proc/xxx output result : x
1701 * 2. #echo x.0x > /proc/xxx output result : x.0x
1702 * 3. #echo x.x0 > /proc/xxx output result : x.x
1703 * 4. #echo x.xx > /proc/xxx output result : x.xx
1704 * Only reserved 2 bits fraction.
1706 for (i = 0; i < (5 - prtn); i++)
1709 frac_bits = min((int)count - prtn, 3 - frac_bits);
1710 prtn += snprintf(buffer + prtn, frac_bits, "%ld",
1711 frac_val * temp_mult / mult);
1714 while(buffer[prtn] < '1' || buffer[prtn] > '9') {
1716 if (buffer[prtn] == '.') {
1723 buffer[prtn++] ='\n';
1727 int lprocfs_seq_read_frac_helper(struct seq_file *m, long val, int mult)
1729 long decimal_val, frac_val;
1731 decimal_val = val / mult;
1732 seq_printf(m, "%ld", decimal_val);
1733 frac_val = val % mult;
1740 /* Three cases: x0, xx, 0x */
1741 if ((frac_val % 10) != 0)
1742 seq_printf(m, ".%ld", frac_val);
1744 seq_printf(m, ".%ld", frac_val / 10);
1747 seq_printf(m, "\n");
1750 EXPORT_SYMBOL(lprocfs_seq_read_frac_helper);
1752 /* Obtains the conversion factor for the unit specified */
1753 static int get_mult(char unit, __u64 *mult)
1758 /* peta, tera, giga, mega, and kilo */
1775 /* some tests expect % to be accepted */
1789 * Ensures the numeric string is valid. The function provides the final
1790 * multiplier in the case a unit exists at the end of the string. It also
1791 * locates the start of the whole and fractional parts (if any). This
1792 * function modifies the string so kstrtoull can be used to parse both
1793 * the whole and fraction portions. This function also figures out
1794 * the base of the number.
1796 static int preprocess_numeric_str(char *buffer, __u64 *mult, __u64 def_mult,
1797 bool allow_units, char **whole, char **frac,
1800 bool hit_decimal = false;
1801 bool hit_unit = false;
1809 /* a hex string if it starts with "0x" */
1810 if (buffer[0] == '0' && tolower(buffer[1]) == 'x') {
1818 /* allow for a single new line before the null terminator */
1819 if (*buffer == '\n') {
1829 /* any chars after our unit indicates a malformed string */
1833 /* ensure we only hit one decimal */
1834 if (*buffer == '.') {
1838 /* if past start, there's a whole part */
1839 if (start != buffer)
1845 } else if (!isdigit(*buffer) &&
1846 !(*base == 16 && isxdigit(*buffer))) {
1848 /* if we allow units, attempt to get mult */
1850 rc = get_mult(*buffer, mult);
1854 /* string stops here, but keep processing */
1866 /* hit a decimal, make sure there's a fractional part */
1872 /* didn't hit a decimal, but may have a whole part */
1873 if (start != buffer && *start)
1877 /* malformed string if we didn't get anything */
1878 if (!*frac && !*whole)
1885 * Parses a numeric string which can contain a whole and fraction portion
1886 * into a __u64. Accepts a multiplier to apply to the value parsed. Also
1887 * allows the string to have a unit at the end. The function handles
1888 * wrapping of the final unsigned value.
1890 static int str_to_u64_parse(char *buffer, unsigned long count,
1891 __u64 *val, __u64 def_mult, bool allow_units)
1895 unsigned int frac_d = 1;
1896 __u64 wrap_indicator = ULLONG_MAX;
1901 unsigned int base = 10;
1903 rc = preprocess_numeric_str(buffer, &mult, def_mult, allow_units,
1904 &strwhole, &strfrac, &base);
1914 /* the multiplier limits how large the value can be */
1915 wrap_indicator /= mult;
1918 rc = kstrtoull(strwhole, base, &whole);
1922 if (whole > wrap_indicator)
1929 if (strlen(strfrac) > 10)
1932 rc = kstrtoull(strfrac, base, &frac);
1936 /* determine power of fractional portion */
1942 /* fractional portion is too large to perform calculation */
1943 if (frac > wrap_indicator)
1947 do_div(frac, frac_d);
1950 /* check that the sum of whole and fraction fits in u64 */
1951 if (whole > (ULLONG_MAX - frac))
1954 *val = whole + frac;
1960 * This function parses numeric/hex strings into __s64. It accepts a multiplier
1961 * which will apply to the value parsed. It also can allow the string to
1962 * have a unit as the last character. The function handles overflow/underflow
1963 * of the signed integer.
1965 static int str_to_s64_internal(const char __user *buffer, unsigned long count,
1966 __s64 *val, __u64 def_mult, bool allow_units)
1970 unsigned int offset = 0;
1971 int signed sign = 1;
1972 __u64 max = LLONG_MAX;
1975 if (count > (sizeof(kernbuf) - 1))
1978 if (copy_from_user(kernbuf, buffer, count))
1981 kernbuf[count] = '\0';
1983 /* keep track of our sign */
1984 if (*kernbuf == '-') {
1987 /* equivalent to max = -LLONG_MIN, avoids overflow */
1991 rc = str_to_u64_parse(kernbuf + offset, count - offset,
1992 &tmp, def_mult, allow_units);
1996 /* check for overflow/underflow */
2000 *val = (__s64)tmp * sign;
2006 * Convert a user string into a signed 64 bit number. This function produces
2007 * an error when the value parsed from the string underflows or
2008 * overflows. This function accepts strings which contain digits and
2009 * optionally a decimal or hex strings which are prefixed with "0x".
2011 * \param[in] buffer string consisting of numbers and optionally a decimal
2012 * \param[in] count buffer length
2013 * \param[in] val if successful, the value represented by the string
2015 * \retval 0 on success
2016 * \retval negative number on error
2018 int lprocfs_str_to_s64(const char __user *buffer, unsigned long count,
2021 return str_to_s64_internal(buffer, count, val, 1, false);
2023 EXPORT_SYMBOL(lprocfs_str_to_s64);
2026 * Convert a user string into a signed 64 bit number. This function produces
2027 * an error when the value parsed from the string times multiplier underflows or
2028 * overflows. This function only accepts strings that contains digits, an
2029 * optional decimal, and a char representing a unit at the end. If a unit is
2030 * specified in the string, the multiplier provided by the caller is ignored.
2031 * This function can also accept hexadecimal strings which are prefixed with
2034 * \param[in] buffer string consisting of numbers, a decimal, and a unit
2035 * \param[in] count buffer length
2036 * \param[in] val if successful, the value represented by the string
2037 * \param[in] defunit default unit if string doesn't contain one
2039 * \retval 0 on success
2040 * \retval negative number on error
2042 int lprocfs_str_with_units_to_s64(const char __user *buffer,
2043 unsigned long count, __s64 *val, char defunit)
2048 if (defunit != '1') {
2049 rc = get_mult(defunit, &mult);
2054 return str_to_s64_internal(buffer, count, val, mult, true);
2056 EXPORT_SYMBOL(lprocfs_str_with_units_to_s64);
2058 static char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
2067 if (!memcmp(s1, s2, l2))
2075 * Find the string \a name in the input \a buffer, and return a pointer to the
2076 * value immediately following \a name, reducing \a count appropriately.
2077 * If \a name is not found the original \a buffer is returned.
2079 char *lprocfs_find_named_value(const char *buffer, const char *name,
2083 size_t buflen = *count;
2085 /* there is no strnstr() in rhel5 and ubuntu kernels */
2086 val = lprocfs_strnstr(buffer, name, buflen);
2088 return (char *)buffer;
2090 val += strlen(name); /* skip prefix */
2091 while (val < buffer + buflen && isspace(*val)) /* skip separator */
2095 while (val < buffer + buflen && isalnum(*val)) {
2100 return val - *count;
2102 EXPORT_SYMBOL(lprocfs_find_named_value);
2104 int lprocfs_seq_create(struct proc_dir_entry *parent,
2107 const struct file_operations *seq_fops,
2110 struct proc_dir_entry *entry;
2113 /* Disallow secretly (un)writable entries. */
2114 LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
2116 entry = proc_create_data(name, mode, parent, seq_fops, data);
2123 EXPORT_SYMBOL(lprocfs_seq_create);
2125 int lprocfs_obd_seq_create(struct obd_device *dev,
2128 const struct file_operations *seq_fops,
2131 return (lprocfs_seq_create(dev->obd_proc_entry, name,
2132 mode, seq_fops, data));
2134 EXPORT_SYMBOL(lprocfs_obd_seq_create);
2136 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
2138 if (value >= OBD_HIST_MAX)
2139 value = OBD_HIST_MAX - 1;
2141 spin_lock(&oh->oh_lock);
2142 oh->oh_buckets[value]++;
2143 spin_unlock(&oh->oh_lock);
2145 EXPORT_SYMBOL(lprocfs_oh_tally);
2147 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
2149 unsigned int val = 0;
2151 if (likely(value != 0))
2152 val = min(fls(value - 1), OBD_HIST_MAX);
2154 lprocfs_oh_tally(oh, val);
2156 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
2158 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
2160 unsigned long ret = 0;
2163 for (i = 0; i < OBD_HIST_MAX; i++)
2164 ret += oh->oh_buckets[i];
2167 EXPORT_SYMBOL(lprocfs_oh_sum);
2169 void lprocfs_oh_clear(struct obd_histogram *oh)
2171 spin_lock(&oh->oh_lock);
2172 memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
2173 spin_unlock(&oh->oh_lock);
2175 EXPORT_SYMBOL(lprocfs_oh_clear);
2177 int lprocfs_obd_rd_max_pages_per_rpc(char *page, char **start, off_t off,
2178 int count, int *eof, void *data)
2180 struct obd_device *dev = data;
2181 struct client_obd *cli = &dev->u.cli;
2184 spin_lock(&cli->cl_loi_list_lock);
2185 rc = snprintf(page, count, "%d\n", cli->cl_max_pages_per_rpc);
2186 spin_unlock(&cli->cl_loi_list_lock);
2191 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
2193 struct obd_device *dev = data;
2194 struct client_obd *cli = &dev->u.cli;
2196 spin_lock(&cli->cl_loi_list_lock);
2197 seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
2198 spin_unlock(&cli->cl_loi_list_lock);
2201 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
2203 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2204 struct root_squash_info *squash, char *name)
2207 char kernbuf[64], *tmp, *errmsg;
2208 unsigned long uid, gid;
2211 if (count >= sizeof(kernbuf)) {
2212 errmsg = "string too long";
2213 GOTO(failed_noprint, rc = -EINVAL);
2215 if (copy_from_user(kernbuf, buffer, count)) {
2216 errmsg = "bad address";
2217 GOTO(failed_noprint, rc = -EFAULT);
2219 kernbuf[count] = '\0';
2221 /* look for uid gid separator */
2222 tmp = strchr(kernbuf, ':');
2224 errmsg = "needs uid:gid format";
2225 GOTO(failed, rc = -EINVAL);
2231 if (kstrtoul(kernbuf, 0, &uid) != 0) {
2233 GOTO(failed, rc = -EINVAL);
2237 if (kstrtoul(tmp, 0, &gid) != 0) {
2239 GOTO(failed, rc = -EINVAL);
2242 squash->rsi_uid = uid;
2243 squash->rsi_gid = gid;
2245 LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2246 name, squash->rsi_uid, squash->rsi_gid);
2254 CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2255 name, kernbuf, errmsg, rc);
2258 CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2262 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2265 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2266 struct root_squash_info *squash, char *name)
2269 char *kernbuf = NULL;
2271 struct list_head tmp;
2276 errmsg = "string too long";
2277 GOTO(failed, rc = -EINVAL);
2280 OBD_ALLOC(kernbuf, count + 1);
2281 if (kernbuf == NULL) {
2282 errmsg = "no memory";
2283 GOTO(failed, rc = -ENOMEM);
2285 if (copy_from_user(kernbuf, buffer, count)) {
2286 errmsg = "bad address";
2287 GOTO(failed, rc = -EFAULT);
2289 kernbuf[count] = '\0';
2291 if (count > 0 && kernbuf[count - 1] == '\n')
2294 if ((len == 4 && strncmp(kernbuf, "NONE", len) == 0) ||
2295 (len == 5 && strncmp(kernbuf, "clear", len) == 0)) {
2296 /* empty string is special case */
2297 down_write(&squash->rsi_sem);
2298 if (!list_empty(&squash->rsi_nosquash_nids))
2299 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2300 up_write(&squash->rsi_sem);
2301 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2302 OBD_FREE(kernbuf, count + 1);
2306 INIT_LIST_HEAD(&tmp);
2307 if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2308 errmsg = "can't parse";
2309 GOTO(failed, rc = -EINVAL);
2311 LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2313 OBD_FREE(kernbuf, count + 1);
2316 down_write(&squash->rsi_sem);
2317 if (!list_empty(&squash->rsi_nosquash_nids))
2318 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2319 list_splice(&tmp, &squash->rsi_nosquash_nids);
2320 up_write(&squash->rsi_sem);
2326 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2327 name, kernbuf, errmsg, rc);
2328 OBD_FREE(kernbuf, count + 1);
2330 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2335 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2337 #endif /* CONFIG_PROC_FS*/