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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/lprocfs_status.c
38 * Author: Hariharan Thantry <thantry@users.sourceforge.net>
41 #define DEBUG_SUBSYSTEM S_CLASS
43 #include <obd_class.h>
44 #include <lprocfs_status.h>
45 #include <lustre/lustre_idl.h>
49 static int lprocfs_no_percpu_stats = 0;
50 module_param(lprocfs_no_percpu_stats, int, 0644);
51 MODULE_PARM_DESC(lprocfs_no_percpu_stats, "Do not alloc percpu data for lprocfs stats");
53 #define MAX_STRING_SIZE 128
55 int lprocfs_single_release(struct inode *inode, struct file *file)
57 return single_release(inode, file);
59 EXPORT_SYMBOL(lprocfs_single_release);
61 int lprocfs_seq_release(struct inode *inode, struct file *file)
63 return seq_release(inode, file);
65 EXPORT_SYMBOL(lprocfs_seq_release);
67 struct proc_dir_entry *
68 lprocfs_add_simple(struct proc_dir_entry *root, char *name,
69 void *data, const struct file_operations *fops)
71 struct proc_dir_entry *proc;
74 if (root == NULL || name == NULL || fops == NULL)
75 return ERR_PTR(-EINVAL);
81 proc = proc_create_data(name, mode, root, fops, data);
83 CERROR("LprocFS: No memory to create /proc entry %s\n",
85 return ERR_PTR(-ENOMEM);
89 EXPORT_SYMBOL(lprocfs_add_simple);
91 struct proc_dir_entry *lprocfs_add_symlink(const char *name,
92 struct proc_dir_entry *parent, const char *format, ...)
94 struct proc_dir_entry *entry;
98 if (parent == NULL || format == NULL)
101 OBD_ALLOC_WAIT(dest, MAX_STRING_SIZE + 1);
105 va_start(ap, format);
106 vsnprintf(dest, MAX_STRING_SIZE, format, ap);
109 entry = proc_symlink(name, parent, dest);
111 CERROR("LprocFS: Could not create symbolic link from "
112 "%s to %s\n", name, dest);
114 OBD_FREE(dest, MAX_STRING_SIZE + 1);
117 EXPORT_SYMBOL(lprocfs_add_symlink);
119 static const struct file_operations lprocfs_generic_fops = { };
124 * \param root [in] The parent proc entry on which new entry will be added.
125 * \param list [in] Array of proc entries to be added.
126 * \param data [in] The argument to be passed when entries read/write routines
127 * are called through /proc file.
129 * \retval 0 on success
133 lprocfs_add_vars(struct proc_dir_entry *root, struct lprocfs_vars *list,
136 if (root == NULL || list == NULL)
139 while (list->name != NULL) {
140 struct proc_dir_entry *proc;
143 if (list->proc_mode != 0000) {
144 mode = list->proc_mode;
145 } else if (list->fops) {
146 if (list->fops->read)
148 if (list->fops->write)
151 proc = proc_create_data(list->name, mode, root,
152 list->fops ?: &lprocfs_generic_fops,
160 EXPORT_SYMBOL(lprocfs_add_vars);
162 #ifndef HAVE_REMOVE_PROC_SUBTREE
163 /* for b=10866, global variable */
164 DECLARE_RWSEM(_lprocfs_lock);
165 EXPORT_SYMBOL(_lprocfs_lock);
167 static void lprocfs_remove_nolock(struct proc_dir_entry **proot)
169 struct proc_dir_entry *root = *proot;
170 struct proc_dir_entry *temp = root;
171 struct proc_dir_entry *rm_entry;
172 struct proc_dir_entry *parent;
175 if (root == NULL || IS_ERR(root))
178 parent = root->parent;
179 LASSERT(parent != NULL);
182 while (temp->subdir != NULL)
188 /* Memory corruption once caused this to fail, and
189 without this LASSERT we would loop here forever. */
190 LASSERTF(strlen(rm_entry->name) == rm_entry->namelen,
191 "0x%p %s/%s len %d\n", rm_entry, temp->name,
192 rm_entry->name, (int)strlen(rm_entry->name));
194 remove_proc_entry(rm_entry->name, temp);
200 int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
202 struct proc_dir_entry *t = NULL;
203 struct proc_dir_entry **p;
206 LASSERT(parent != NULL);
209 down_write(&_lprocfs_lock);
210 /* lookup target name */
211 for (p = &parent->subdir; *p; p = &(*p)->next) {
212 if ((*p)->namelen != len)
214 if (memcmp(name, (*p)->name, len))
221 /* verify it's empty: do not count "num_refs" */
222 for (p = &t->subdir; *p; p = &(*p)->next) {
223 if ((*p)->namelen != strlen("num_refs")) {
227 if (memcmp("num_refs", (*p)->name,
228 strlen("num_refs"))) {
236 lprocfs_remove_nolock(&t);
238 up_write(&_lprocfs_lock);
241 #endif /* !HAVE_REMOVE_PROC_SUBTREE */
243 #ifndef HAVE_PROC_REMOVE
244 void proc_remove(struct proc_dir_entry *de)
246 #ifndef HAVE_REMOVE_PROC_SUBTREE
247 down_write(&_lprocfs_lock); /* search vs remove race */
248 lprocfs_remove_nolock(&de);
249 up_write(&_lprocfs_lock);
252 remove_proc_subtree(de->name, de->parent);
257 void lprocfs_remove(struct proc_dir_entry **rooth)
262 EXPORT_SYMBOL(lprocfs_remove);
264 void lprocfs_remove_proc_entry(const char *name, struct proc_dir_entry *parent)
266 LASSERT(parent != NULL);
267 remove_proc_entry(name, parent);
269 EXPORT_SYMBOL(lprocfs_remove_proc_entry);
271 struct proc_dir_entry *
272 lprocfs_register(const char *name, struct proc_dir_entry *parent,
273 struct lprocfs_vars *list, void *data)
275 struct proc_dir_entry *newchild;
277 newchild = proc_mkdir(name, parent);
278 if (newchild == NULL)
279 return ERR_PTR(-ENOMEM);
282 int rc = lprocfs_add_vars(newchild, list, data);
284 lprocfs_remove(&newchild);
290 EXPORT_SYMBOL(lprocfs_register);
292 /* Generic callbacks */
293 int lprocfs_uint_seq_show(struct seq_file *m, void *data)
295 seq_printf(m, "%u\n", *(unsigned int *)data);
298 EXPORT_SYMBOL(lprocfs_uint_seq_show);
300 int lprocfs_wr_uint(struct file *file, const char __user *buffer,
301 unsigned long count, void *data)
304 char dummy[MAX_STRING_SIZE + 1], *end;
307 dummy[MAX_STRING_SIZE] = '\0';
308 if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
311 tmp = simple_strtoul(dummy, &end, 0);
315 *p = (unsigned int)tmp;
318 EXPORT_SYMBOL(lprocfs_wr_uint);
320 ssize_t lprocfs_uint_seq_write(struct file *file, const char __user *buffer,
321 size_t count, loff_t *off)
323 int *data = ((struct seq_file *)file->private_data)->private;
327 rc = lprocfs_str_to_s64(buffer, count, &val);
331 return lprocfs_wr_uint(file, buffer, count, data);
333 EXPORT_SYMBOL(lprocfs_uint_seq_write);
335 int lprocfs_u64_seq_show(struct seq_file *m, void *data)
337 LASSERT(data != NULL);
338 seq_printf(m, LPU64"\n", *(__u64 *)data);
341 EXPORT_SYMBOL(lprocfs_u64_seq_show);
343 int lprocfs_atomic_seq_show(struct seq_file *m, void *data)
345 atomic_t *atom = data;
346 LASSERT(atom != NULL);
347 seq_printf(m, "%d\n", atomic_read(atom));
350 EXPORT_SYMBOL(lprocfs_atomic_seq_show);
353 lprocfs_atomic_seq_write(struct file *file, const char __user *buffer,
354 size_t count, loff_t *off)
356 atomic_t *atm = ((struct seq_file *)file->private_data)->private;
360 rc = lprocfs_str_to_s64(buffer, count, &val);
364 if (val <= 0 || val > INT_MAX)
367 atomic_set(atm, val);
370 EXPORT_SYMBOL(lprocfs_atomic_seq_write);
372 int lprocfs_uuid_seq_show(struct seq_file *m, void *data)
374 struct obd_device *obd = data;
376 LASSERT(obd != NULL);
377 seq_printf(m, "%s\n", obd->obd_uuid.uuid);
380 EXPORT_SYMBOL(lprocfs_uuid_seq_show);
382 int lprocfs_name_seq_show(struct seq_file *m, void *data)
384 struct obd_device *dev = data;
386 LASSERT(dev != NULL);
387 seq_printf(m, "%s\n", dev->obd_name);
390 EXPORT_SYMBOL(lprocfs_name_seq_show);
392 int lprocfs_blksize_seq_show(struct seq_file *m, void *data)
394 struct obd_device *obd = data;
395 struct obd_statfs osfs;
396 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
397 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
400 seq_printf(m, "%u\n", osfs.os_bsize);
403 EXPORT_SYMBOL(lprocfs_blksize_seq_show);
405 int lprocfs_kbytestotal_seq_show(struct seq_file *m, void *data)
407 struct obd_device *obd = data;
408 struct obd_statfs osfs;
409 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
410 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
413 __u32 blk_size = osfs.os_bsize >> 10;
414 __u64 result = osfs.os_blocks;
416 while (blk_size >>= 1)
419 seq_printf(m, LPU64"\n", result);
423 EXPORT_SYMBOL(lprocfs_kbytestotal_seq_show);
425 int lprocfs_kbytesfree_seq_show(struct seq_file *m, void *data)
427 struct obd_device *obd = data;
428 struct obd_statfs osfs;
429 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
430 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
433 __u32 blk_size = osfs.os_bsize >> 10;
434 __u64 result = osfs.os_bfree;
436 while (blk_size >>= 1)
439 seq_printf(m, LPU64"\n", result);
443 EXPORT_SYMBOL(lprocfs_kbytesfree_seq_show);
445 int lprocfs_kbytesavail_seq_show(struct seq_file *m, void *data)
447 struct obd_device *obd = data;
448 struct obd_statfs osfs;
449 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
450 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
453 __u32 blk_size = osfs.os_bsize >> 10;
454 __u64 result = osfs.os_bavail;
456 while (blk_size >>= 1)
459 seq_printf(m, LPU64"\n", result);
463 EXPORT_SYMBOL(lprocfs_kbytesavail_seq_show);
465 int lprocfs_filestotal_seq_show(struct seq_file *m, void *data)
467 struct obd_device *obd = data;
468 struct obd_statfs osfs;
469 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
470 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
473 seq_printf(m, LPU64"\n", osfs.os_files);
476 EXPORT_SYMBOL(lprocfs_filestotal_seq_show);
478 int lprocfs_filesfree_seq_show(struct seq_file *m, void *data)
480 struct obd_device *obd = data;
481 struct obd_statfs osfs;
482 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
483 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
486 seq_printf(m, LPU64"\n", osfs.os_ffree);
489 EXPORT_SYMBOL(lprocfs_filesfree_seq_show);
491 int lprocfs_server_uuid_seq_show(struct seq_file *m, void *data)
493 struct obd_device *obd = data;
494 struct obd_import *imp;
495 char *imp_state_name = NULL;
498 LASSERT(obd != NULL);
499 LPROCFS_CLIMP_CHECK(obd);
500 imp = obd->u.cli.cl_import;
501 imp_state_name = ptlrpc_import_state_name(imp->imp_state);
502 seq_printf(m, "%s\t%s%s\n", obd2cli_tgt(obd), imp_state_name,
503 imp->imp_deactive ? "\tDEACTIVATED" : "");
505 LPROCFS_CLIMP_EXIT(obd);
508 EXPORT_SYMBOL(lprocfs_server_uuid_seq_show);
510 int lprocfs_conn_uuid_seq_show(struct seq_file *m, void *data)
512 struct obd_device *obd = data;
513 struct ptlrpc_connection *conn;
516 LASSERT(obd != NULL);
518 LPROCFS_CLIMP_CHECK(obd);
519 conn = obd->u.cli.cl_import->imp_connection;
520 if (conn && obd->u.cli.cl_import)
521 seq_printf(m, "%s\n", conn->c_remote_uuid.uuid);
523 seq_printf(m, "%s\n", "<none>");
525 LPROCFS_CLIMP_EXIT(obd);
528 EXPORT_SYMBOL(lprocfs_conn_uuid_seq_show);
530 /** add up per-cpu counters */
531 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
532 struct lprocfs_counter *cnt)
534 unsigned int num_entry;
535 struct lprocfs_counter *percpu_cntr;
537 unsigned long flags = 0;
539 memset(cnt, 0, sizeof(*cnt));
542 /* set count to 1 to avoid divide-by-zero errs in callers */
547 cnt->lc_min = LC_MIN_INIT;
549 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
551 for (i = 0; i < num_entry; i++) {
552 if (stats->ls_percpu[i] == NULL)
554 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
556 cnt->lc_count += percpu_cntr->lc_count;
557 cnt->lc_sum += percpu_cntr->lc_sum;
558 if (percpu_cntr->lc_min < cnt->lc_min)
559 cnt->lc_min = percpu_cntr->lc_min;
560 if (percpu_cntr->lc_max > cnt->lc_max)
561 cnt->lc_max = percpu_cntr->lc_max;
562 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
565 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
569 * Append a space separated list of current set flags to str.
571 #define flag2str(flag) \
573 if (imp->imp_##flag) { \
574 seq_printf(m, "%s" #flag, first ? "" : ", "); \
578 static void obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
582 if (imp->imp_obd->obd_no_recov) {
583 seq_printf(m, "no_recov");
589 flag2str(replayable);
590 flag2str(delayed_recovery);
591 flag2str(no_lock_replay);
592 flag2str(vbr_failed);
594 flag2str(resend_replay);
595 flag2str(no_pinger_recover);
596 flag2str(need_mne_swab);
597 flag2str(connect_tried);
601 static const char *obd_connect_names[] = {
616 "join_file(obsolete)",
620 "remote_client_by_force",
629 "mds_mds_connection",
632 "alt_checksum_algorithm",
672 static void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags,
673 __u64 flags2, const char *sep)
679 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
681 seq_printf(m, "%s%s",
682 first ? "" : sep, obd_connect_names[i]);
687 if (flags & ~(mask - 1)) {
688 seq_printf(m, "%sunknown_"LPX64,
689 first ? "" : sep, flags & ~(mask - 1));
693 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
696 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
698 seq_printf(m, "%s%s",
699 first ? "" : sep, obd_connect_names[i]);
704 if (flags2 & ~(mask - 1)) {
705 seq_printf(m, "%sunknown2_"LPX64,
706 first ? "" : sep, flags2 & ~(mask - 1));
711 int obd_connect_flags2str(char *page, int count, __u64 flags, __u64 flags2,
717 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
719 ret += snprintf(page + ret, count - ret, "%s%s",
720 ret ? sep : "", obd_connect_names[i]);
723 if (flags & ~(mask - 1))
724 ret += snprintf(page + ret, count - ret,
726 ret ? sep : "", flags & ~(mask - 1));
728 if (!(flags & OBD_CONNECT_FLAGS2) || flags2 == 0)
731 for (i = 64, mask = 1; obd_connect_names[i] != NULL; i++, mask <<= 1) {
733 ret += snprintf(page + ret, count - ret, "%s%s",
734 ret ? sep : "", obd_connect_names[i]);
737 if (flags2 & ~(mask - 1))
738 ret += snprintf(page + ret, count - ret,
740 ret ? sep : "", flags2 & ~(mask - 1));
744 EXPORT_SYMBOL(obd_connect_flags2str);
746 static void obd_connect_data_seqprint(struct seq_file *m,
747 struct obd_connect_data *ocd)
751 LASSERT(ocd != NULL);
752 flags = ocd->ocd_connect_flags;
754 seq_printf(m, " connect_data:\n"
757 ocd->ocd_connect_flags,
759 if (flags & OBD_CONNECT_VERSION)
760 seq_printf(m, " target_version: %u.%u.%u.%u\n",
761 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
762 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
763 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
764 OBD_OCD_VERSION_FIX(ocd->ocd_version));
765 if (flags & OBD_CONNECT_MDS)
766 seq_printf(m, " mdt_index: %d\n", ocd->ocd_group);
767 if (flags & OBD_CONNECT_GRANT)
768 seq_printf(m, " initial_grant: %d\n", ocd->ocd_grant);
769 if (flags & OBD_CONNECT_INDEX)
770 seq_printf(m, " target_index: %u\n", ocd->ocd_index);
771 if (flags & OBD_CONNECT_BRW_SIZE)
772 seq_printf(m, " max_brw_size: %d\n", ocd->ocd_brw_size);
773 if (flags & OBD_CONNECT_IBITS)
774 seq_printf(m, " ibits_known: "LPX64"\n",
775 ocd->ocd_ibits_known);
776 if (flags & OBD_CONNECT_GRANT_PARAM)
777 seq_printf(m, " grant_block_size: %d\n"
778 " grant_inode_size: %d\n"
779 " grant_max_extent_size: %d\n"
780 " grant_extent_tax: %d\n",
781 1 << ocd->ocd_grant_blkbits,
782 1 << ocd->ocd_grant_inobits,
783 ocd->ocd_grant_max_blks << ocd->ocd_grant_blkbits,
784 ocd->ocd_grant_tax_kb << 10);
785 if (flags & OBD_CONNECT_TRANSNO)
786 seq_printf(m, " first_transno: "LPX64"\n",
788 if (flags & OBD_CONNECT_CKSUM)
789 seq_printf(m, " cksum_types: %#x\n",
790 ocd->ocd_cksum_types);
791 if (flags & OBD_CONNECT_MAX_EASIZE)
792 seq_printf(m, " max_easize: %d\n", ocd->ocd_max_easize);
793 if (flags & OBD_CONNECT_MAXBYTES)
794 seq_printf(m, " max_object_bytes: "LPU64"\n",
796 if (flags & OBD_CONNECT_MULTIMODRPCS)
797 seq_printf(m, " max_mod_rpcs: %hu\n",
798 ocd->ocd_maxmodrpcs);
801 int lprocfs_import_seq_show(struct seq_file *m, void *data)
803 char nidstr[LNET_NIDSTR_SIZE];
804 struct lprocfs_counter ret;
805 struct lprocfs_counter_header *header;
806 struct obd_device *obd = (struct obd_device *)data;
807 struct obd_import *imp;
808 struct obd_import_conn *conn;
809 struct obd_connect_data *ocd;
814 LASSERT(obd != NULL);
815 LPROCFS_CLIMP_CHECK(obd);
816 imp = obd->u.cli.cl_import;
817 ocd = &imp->imp_connect_data;
819 seq_printf(m, "import:\n"
823 " connect_flags: [ ",
826 ptlrpc_import_state_name(imp->imp_state));
827 obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
828 imp->imp_connect_data.ocd_connect_flags2,
830 seq_printf(m, " ]\n");
831 obd_connect_data_seqprint(m, ocd);
832 seq_printf(m, " import_flags: [ ");
833 obd_import_flags2str(imp, m);
837 " failover_nids: [ ");
838 spin_lock(&imp->imp_lock);
840 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
841 libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
842 nidstr, sizeof(nidstr));
843 seq_printf(m, "%s%s", j ? ", " : "", nidstr);
846 if (imp->imp_connection != NULL)
847 libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
848 nidstr, sizeof(nidstr));
850 strncpy(nidstr, "<none>", sizeof(nidstr));
852 " current_connection: %s\n"
853 " connection_attempts: %u\n"
855 " in-progress_invalidations: %u\n",
859 atomic_read(&imp->imp_inval_count));
860 spin_unlock(&imp->imp_lock);
862 if (obd->obd_svc_stats == NULL)
865 header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
866 lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
867 if (ret.lc_count != 0) {
868 /* first argument to do_div MUST be __u64 */
869 __u64 sum = ret.lc_sum;
870 do_div(sum, ret.lc_count);
874 seq_printf(m, " rpcs:\n"
876 " unregistering: %u\n"
878 " avg_waittime: "LPU64" %s\n",
879 atomic_read(&imp->imp_inflight),
880 atomic_read(&imp->imp_unregistering),
881 atomic_read(&imp->imp_timeouts),
882 ret.lc_sum, header->lc_units);
885 for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
886 if (imp->imp_at.iat_portal[j] == 0)
888 k = max_t(unsigned int, k,
889 at_get(&imp->imp_at.iat_service_estimate[j]));
891 seq_printf(m, " service_estimates:\n"
892 " services: %u sec\n"
893 " network: %u sec\n",
895 at_get(&imp->imp_at.iat_net_latency));
897 seq_printf(m, " transactions:\n"
898 " last_replay: "LPU64"\n"
899 " peer_committed: "LPU64"\n"
900 " last_checked: "LPU64"\n",
901 imp->imp_last_replay_transno,
902 imp->imp_peer_committed_transno,
903 imp->imp_last_transno_checked);
906 for (rw = 0; rw <= 1; rw++) {
907 lprocfs_stats_collect(obd->obd_svc_stats,
908 PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
910 if (ret.lc_sum > 0 && ret.lc_count > 0) {
911 /* first argument to do_div MUST be __u64 */
912 __u64 sum = ret.lc_sum;
913 do_div(sum, ret.lc_count);
915 seq_printf(m, " %s_data_averages:\n"
916 " bytes_per_rpc: "LPU64"\n",
917 rw ? "write" : "read",
921 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
922 header = &obd->obd_svc_stats->ls_cnt_header[j];
923 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
924 if (ret.lc_sum > 0 && ret.lc_count != 0) {
925 /* first argument to do_div MUST be __u64 */
926 __u64 sum = ret.lc_sum;
927 do_div(sum, ret.lc_count);
929 seq_printf(m, " %s_per_rpc: "LPU64"\n",
930 header->lc_units, ret.lc_sum);
933 seq_printf(m, " MB_per_sec: %u.%.02u\n",
934 k / j, (100 * k / j) % 100);
939 LPROCFS_CLIMP_EXIT(obd);
942 EXPORT_SYMBOL(lprocfs_import_seq_show);
944 int lprocfs_state_seq_show(struct seq_file *m, void *data)
946 struct obd_device *obd = (struct obd_device *)data;
947 struct obd_import *imp;
950 LASSERT(obd != NULL);
951 LPROCFS_CLIMP_CHECK(obd);
952 imp = obd->u.cli.cl_import;
954 seq_printf(m, "current_state: %s\n",
955 ptlrpc_import_state_name(imp->imp_state));
956 seq_printf(m, "state_history:\n");
957 k = imp->imp_state_hist_idx;
958 for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
959 struct import_state_hist *ish =
960 &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
961 if (ish->ish_state == 0)
963 seq_printf(m, " - [ "CFS_TIME_T", %s ]\n",
965 ptlrpc_import_state_name(ish->ish_state));
968 LPROCFS_CLIMP_EXIT(obd);
971 EXPORT_SYMBOL(lprocfs_state_seq_show);
973 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
976 for (i = 0; i < AT_BINS; i++)
977 seq_printf(m, "%3u ", at->at_hist[i]);
981 EXPORT_SYMBOL(lprocfs_at_hist_helper);
983 /* See also ptlrpc_lprocfs_timeouts_show_seq */
984 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
986 struct obd_device *obd = (struct obd_device *)data;
987 struct obd_import *imp;
988 unsigned int cur, worst;
993 LASSERT(obd != NULL);
994 LPROCFS_CLIMP_CHECK(obd);
995 imp = obd->u.cli.cl_import;
997 now = cfs_time_current_sec();
999 /* Some network health info for kicks */
1000 s2dhms(&ts, now - imp->imp_last_reply_time);
1001 seq_printf(m, "%-10s : %ld, "DHMS_FMT" ago\n",
1002 "last reply", imp->imp_last_reply_time, DHMS_VARS(&ts));
1004 cur = at_get(&imp->imp_at.iat_net_latency);
1005 worst = imp->imp_at.iat_net_latency.at_worst_ever;
1006 worstt = imp->imp_at.iat_net_latency.at_worst_time;
1007 s2dhms(&ts, now - worstt);
1008 seq_printf(m, "%-10s : cur %3u worst %3u (at %ld, "DHMS_FMT" ago) ",
1009 "network", cur, worst, worstt, DHMS_VARS(&ts));
1010 lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
1012 for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1013 if (imp->imp_at.iat_portal[i] == 0)
1015 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
1016 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
1017 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
1018 s2dhms(&ts, now - worstt);
1019 seq_printf(m, "portal %-2d : cur %3u worst %3u (at %ld, "
1020 DHMS_FMT" ago) ", imp->imp_at.iat_portal[i],
1021 cur, worst, worstt, DHMS_VARS(&ts));
1022 lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
1025 LPROCFS_CLIMP_EXIT(obd);
1028 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
1030 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
1032 struct obd_device *obd = data;
1036 LPROCFS_CLIMP_CHECK(obd);
1037 flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
1038 flags2 = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags2;
1039 seq_printf(m, "flags="LPX64"\n", flags);
1040 seq_printf(m, "flags2="LPX64"\n", flags2);
1041 obd_connect_seq_flags2str(m, flags, flags2, "\n");
1042 seq_printf(m, "\n");
1043 LPROCFS_CLIMP_EXIT(obd);
1046 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
1049 lprocfs_obd_setup(struct obd_device *obd)
1053 LASSERT(obd != NULL);
1054 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1055 LASSERT(obd->obd_type->typ_procroot != NULL);
1057 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1058 obd->obd_type->typ_procroot,
1059 obd->obd_vars, obd);
1060 if (IS_ERR(obd->obd_proc_entry)) {
1061 rc = PTR_ERR(obd->obd_proc_entry);
1062 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1063 obd->obd_proc_entry = NULL;
1067 EXPORT_SYMBOL(lprocfs_obd_setup);
1069 int lprocfs_obd_cleanup(struct obd_device *obd)
1073 if (obd->obd_proc_exports_entry) {
1074 /* Should be no exports left */
1075 lprocfs_remove(&obd->obd_proc_exports_entry);
1076 obd->obd_proc_exports_entry = NULL;
1078 if (obd->obd_proc_entry) {
1079 lprocfs_remove(&obd->obd_proc_entry);
1080 obd->obd_proc_entry = NULL;
1084 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1086 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1088 struct lprocfs_counter *cntr;
1089 unsigned int percpusize;
1091 unsigned long flags = 0;
1094 LASSERT(stats->ls_percpu[cpuid] == NULL);
1095 LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1097 percpusize = lprocfs_stats_counter_size(stats);
1098 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1099 if (stats->ls_percpu[cpuid] != NULL) {
1101 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1102 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1103 spin_lock_irqsave(&stats->ls_lock, flags);
1105 spin_lock(&stats->ls_lock);
1106 if (stats->ls_biggest_alloc_num <= cpuid)
1107 stats->ls_biggest_alloc_num = cpuid + 1;
1108 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1109 spin_unlock_irqrestore(&stats->ls_lock, flags);
1111 spin_unlock(&stats->ls_lock);
1114 /* initialize the ls_percpu[cpuid] non-zero counter */
1115 for (i = 0; i < stats->ls_num; ++i) {
1116 cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1117 cntr->lc_min = LC_MIN_INIT;
1122 EXPORT_SYMBOL(lprocfs_stats_alloc_one);
1124 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1125 enum lprocfs_stats_flags flags)
1127 struct lprocfs_stats *stats;
1128 unsigned int num_entry;
1129 unsigned int percpusize = 0;
1135 if (lprocfs_no_percpu_stats != 0)
1136 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1138 if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1141 num_entry = num_possible_cpus();
1143 /* alloc percpu pointers for all possible cpu slots */
1144 LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1148 stats->ls_num = num;
1149 stats->ls_flags = flags;
1150 spin_lock_init(&stats->ls_lock);
1152 /* alloc num of counter headers */
1153 LIBCFS_ALLOC(stats->ls_cnt_header,
1154 stats->ls_num * sizeof(struct lprocfs_counter_header));
1155 if (stats->ls_cnt_header == NULL)
1158 if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1159 /* contains only one set counters */
1160 percpusize = lprocfs_stats_counter_size(stats);
1161 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1162 if (stats->ls_percpu[0] == NULL)
1164 stats->ls_biggest_alloc_num = 1;
1165 } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1166 /* alloc all percpu data, currently only obd_memory use this */
1167 for (i = 0; i < num_entry; ++i)
1168 if (lprocfs_stats_alloc_one(stats, i) < 0)
1175 lprocfs_free_stats(&stats);
1178 EXPORT_SYMBOL(lprocfs_alloc_stats);
1180 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1182 struct lprocfs_stats *stats = *statsh;
1183 unsigned int num_entry;
1184 unsigned int percpusize;
1187 if (stats == NULL || stats->ls_num == 0)
1191 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1194 num_entry = num_possible_cpus();
1196 percpusize = lprocfs_stats_counter_size(stats);
1197 for (i = 0; i < num_entry; i++)
1198 if (stats->ls_percpu[i] != NULL)
1199 LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1200 if (stats->ls_cnt_header != NULL)
1201 LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
1202 sizeof(struct lprocfs_counter_header));
1203 LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1205 EXPORT_SYMBOL(lprocfs_free_stats);
1207 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1209 struct lprocfs_counter *percpu_cntr;
1212 unsigned int num_entry;
1213 unsigned long flags = 0;
1215 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1217 for (i = 0; i < num_entry; i++) {
1218 if (stats->ls_percpu[i] == NULL)
1220 for (j = 0; j < stats->ls_num; j++) {
1221 percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1222 percpu_cntr->lc_count = 0;
1223 percpu_cntr->lc_min = LC_MIN_INIT;
1224 percpu_cntr->lc_max = 0;
1225 percpu_cntr->lc_sumsquare = 0;
1226 percpu_cntr->lc_sum = 0;
1227 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1228 percpu_cntr->lc_sum_irq = 0;
1232 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1234 EXPORT_SYMBOL(lprocfs_clear_stats);
1236 static ssize_t lprocfs_stats_seq_write(struct file *file,
1237 const char __user *buf,
1238 size_t len, loff_t *off)
1240 struct seq_file *seq = file->private_data;
1241 struct lprocfs_stats *stats = seq->private;
1243 lprocfs_clear_stats(stats);
1248 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1250 struct lprocfs_stats *stats = p->private;
1252 return (*pos < stats->ls_num) ? pos : NULL;
1255 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1259 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1263 return lprocfs_stats_seq_start(p, pos);
1266 /* seq file export of one lprocfs counter */
1267 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1269 struct lprocfs_stats *stats = p->private;
1270 struct lprocfs_counter_header *hdr;
1271 struct lprocfs_counter ctr;
1272 int idx = *(loff_t *)v;
1277 do_gettimeofday(&now);
1278 seq_printf(p, "%-25s %lu.%lu secs.usecs\n",
1279 "snapshot_time", now.tv_sec, now.tv_usec);
1282 hdr = &stats->ls_cnt_header[idx];
1283 lprocfs_stats_collect(stats, idx, &ctr);
1285 if (ctr.lc_count == 0)
1288 seq_printf(p, "%-25s "LPD64" samples [%s]", hdr->lc_name,
1289 ctr.lc_count, hdr->lc_units);
1291 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1292 seq_printf(p, " "LPD64" "LPD64" "LPD64,
1293 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1294 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1295 seq_printf(p, " "LPD64, ctr.lc_sumsquare);
1301 static const struct seq_operations lprocfs_stats_seq_sops = {
1302 .start = lprocfs_stats_seq_start,
1303 .stop = lprocfs_stats_seq_stop,
1304 .next = lprocfs_stats_seq_next,
1305 .show = lprocfs_stats_seq_show,
1308 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1310 struct seq_file *seq;
1313 rc = LPROCFS_ENTRY_CHECK(inode);
1317 rc = seq_open(file, &lprocfs_stats_seq_sops);
1320 seq = file->private_data;
1321 seq->private = PDE_DATA(inode);
1325 static const struct file_operations lprocfs_stats_seq_fops = {
1326 .owner = THIS_MODULE,
1327 .open = lprocfs_stats_seq_open,
1329 .write = lprocfs_stats_seq_write,
1330 .llseek = seq_lseek,
1331 .release = lprocfs_seq_release,
1334 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1335 struct lprocfs_stats *stats)
1337 struct proc_dir_entry *entry;
1338 LASSERT(root != NULL);
1340 entry = proc_create_data(name, 0644, root,
1341 &lprocfs_stats_seq_fops, stats);
1346 EXPORT_SYMBOL(lprocfs_register_stats);
1348 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1349 unsigned conf, const char *name, const char *units)
1351 struct lprocfs_counter_header *header;
1352 struct lprocfs_counter *percpu_cntr;
1353 unsigned long flags = 0;
1355 unsigned int num_cpu;
1357 LASSERT(stats != NULL);
1359 header = &stats->ls_cnt_header[index];
1360 LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1361 index, name, units);
1363 header->lc_config = conf;
1364 header->lc_name = name;
1365 header->lc_units = units;
1367 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1368 for (i = 0; i < num_cpu; ++i) {
1369 if (stats->ls_percpu[i] == NULL)
1371 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1372 percpu_cntr->lc_count = 0;
1373 percpu_cntr->lc_min = LC_MIN_INIT;
1374 percpu_cntr->lc_max = 0;
1375 percpu_cntr->lc_sumsquare = 0;
1376 percpu_cntr->lc_sum = 0;
1377 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1378 percpu_cntr->lc_sum_irq = 0;
1380 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1382 EXPORT_SYMBOL(lprocfs_counter_init);
1384 /* Note that we only init md counters for ops whose offset is less
1385 * than NUM_MD_STATS. This is explained in a comment in the definition
1386 * of struct md_ops. */
1387 #define LPROCFS_MD_OP_INIT(base, stats, op) \
1389 unsigned int _idx = base + MD_COUNTER_OFFSET(op); \
1391 if (MD_COUNTER_OFFSET(op) < NUM_MD_STATS) { \
1392 LASSERT(_idx < stats->ls_num); \
1393 lprocfs_counter_init(stats, _idx, 0, #op, "reqs"); \
1397 void lprocfs_init_mps_stats(int num_private_stats, struct lprocfs_stats *stats)
1399 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_root);
1400 LPROCFS_MD_OP_INIT(num_private_stats, stats, null_inode);
1401 LPROCFS_MD_OP_INIT(num_private_stats, stats, close);
1402 LPROCFS_MD_OP_INIT(num_private_stats, stats, create);
1403 LPROCFS_MD_OP_INIT(num_private_stats, stats, enqueue);
1404 LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr);
1405 LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr_name);
1406 LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_lock);
1407 LPROCFS_MD_OP_INIT(num_private_stats, stats, link);
1408 LPROCFS_MD_OP_INIT(num_private_stats, stats, rename);
1409 LPROCFS_MD_OP_INIT(num_private_stats, stats, setattr);
1410 LPROCFS_MD_OP_INIT(num_private_stats, stats, fsync);
1411 LPROCFS_MD_OP_INIT(num_private_stats, stats, read_page);
1412 LPROCFS_MD_OP_INIT(num_private_stats, stats, unlink);
1413 LPROCFS_MD_OP_INIT(num_private_stats, stats, setxattr);
1414 LPROCFS_MD_OP_INIT(num_private_stats, stats, getxattr);
1415 LPROCFS_MD_OP_INIT(num_private_stats, stats, init_ea_size);
1416 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_lustre_md);
1417 LPROCFS_MD_OP_INIT(num_private_stats, stats, free_lustre_md);
1418 LPROCFS_MD_OP_INIT(num_private_stats, stats, merge_attr);
1419 LPROCFS_MD_OP_INIT(num_private_stats, stats, set_open_replay_data);
1420 LPROCFS_MD_OP_INIT(num_private_stats, stats, clear_open_replay_data);
1421 LPROCFS_MD_OP_INIT(num_private_stats, stats, set_lock_data);
1422 LPROCFS_MD_OP_INIT(num_private_stats, stats, lock_match);
1423 LPROCFS_MD_OP_INIT(num_private_stats, stats, cancel_unused);
1424 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_remote_perm);
1425 LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_getattr_async);
1426 LPROCFS_MD_OP_INIT(num_private_stats, stats, revalidate_lock);
1429 int lprocfs_alloc_md_stats(struct obd_device *obd,
1430 unsigned int num_private_stats)
1432 struct lprocfs_stats *stats;
1433 unsigned int num_stats;
1436 CLASSERT(offsetof(struct md_ops, MD_STATS_FIRST_OP) == 0);
1437 CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_FIRST_OP) == 0);
1438 CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_LAST_OP) > 0);
1440 /* TODO Ensure that this function is only used where
1441 * appropriate by adding an assertion to the effect that
1442 * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1443 * because mdt_procfs_init() uses this function to allocate
1444 * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1445 * mdt layer does not use the md_ops interface. This is
1446 * confusing and a waste of memory. See LU-2484.
1448 LASSERT(obd->obd_proc_entry != NULL);
1449 LASSERT(obd->obd_md_stats == NULL);
1450 LASSERT(obd->obd_md_cntr_base == 0);
1452 num_stats = NUM_MD_STATS + num_private_stats;
1453 stats = lprocfs_alloc_stats(num_stats, 0);
1457 lprocfs_init_mps_stats(num_private_stats, stats);
1459 for (i = num_private_stats; i < num_stats; i++) {
1460 if (stats->ls_cnt_header[i].lc_name == NULL) {
1461 CERROR("Missing md_stat initializer md_op "
1462 "operation at offset %d. Aborting.\n",
1463 i - num_private_stats);
1468 rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1470 lprocfs_free_stats(&stats);
1472 obd->obd_md_stats = stats;
1473 obd->obd_md_cntr_base = num_private_stats;
1478 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1480 void lprocfs_free_md_stats(struct obd_device *obd)
1482 struct lprocfs_stats *stats = obd->obd_md_stats;
1484 if (stats != NULL) {
1485 obd->obd_md_stats = NULL;
1486 obd->obd_md_cntr_base = 0;
1487 lprocfs_free_stats(&stats);
1490 EXPORT_SYMBOL(lprocfs_free_md_stats);
1492 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1494 lprocfs_counter_init(ldlm_stats,
1495 LDLM_ENQUEUE - LDLM_FIRST_OPC,
1496 0, "ldlm_enqueue", "reqs");
1497 lprocfs_counter_init(ldlm_stats,
1498 LDLM_CONVERT - LDLM_FIRST_OPC,
1499 0, "ldlm_convert", "reqs");
1500 lprocfs_counter_init(ldlm_stats,
1501 LDLM_CANCEL - LDLM_FIRST_OPC,
1502 0, "ldlm_cancel", "reqs");
1503 lprocfs_counter_init(ldlm_stats,
1504 LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1505 0, "ldlm_bl_callback", "reqs");
1506 lprocfs_counter_init(ldlm_stats,
1507 LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1508 0, "ldlm_cp_callback", "reqs");
1509 lprocfs_counter_init(ldlm_stats,
1510 LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1511 0, "ldlm_gl_callback", "reqs");
1513 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1515 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1516 struct lprocfs_counter_header *header,
1517 enum lprocfs_stats_flags flags,
1518 enum lprocfs_fields_flags field)
1522 if (lc == NULL || header == NULL)
1526 case LPROCFS_FIELDS_FLAGS_CONFIG:
1527 ret = header->lc_config;
1529 case LPROCFS_FIELDS_FLAGS_SUM:
1531 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1532 ret += lc->lc_sum_irq;
1534 case LPROCFS_FIELDS_FLAGS_MIN:
1537 case LPROCFS_FIELDS_FLAGS_MAX:
1540 case LPROCFS_FIELDS_FLAGS_AVG:
1541 ret = (lc->lc_max - lc->lc_min) / 2;
1543 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1544 ret = lc->lc_sumsquare;
1546 case LPROCFS_FIELDS_FLAGS_COUNT:
1554 EXPORT_SYMBOL(lprocfs_read_helper);
1556 int lprocfs_read_frac_helper(char *buffer, unsigned long count, long val,
1559 long decimal_val, frac_val;
1565 decimal_val = val / mult;
1566 prtn = snprintf(buffer, count, "%ld", decimal_val);
1567 frac_val = val % mult;
1569 if (prtn < (count - 4) && frac_val > 0) {
1571 int i, temp_mult = 1, frac_bits = 0;
1573 temp_frac = frac_val * 10;
1574 buffer[prtn++] = '.';
1575 while (frac_bits < 2 && (temp_frac / mult) < 1 ) {
1576 /* only reserved 2 bits fraction */
1577 buffer[prtn++] ='0';
1582 * Need to think these cases :
1583 * 1. #echo x.00 > /proc/xxx output result : x
1584 * 2. #echo x.0x > /proc/xxx output result : x.0x
1585 * 3. #echo x.x0 > /proc/xxx output result : x.x
1586 * 4. #echo x.xx > /proc/xxx output result : x.xx
1587 * Only reserved 2 bits fraction.
1589 for (i = 0; i < (5 - prtn); i++)
1592 frac_bits = min((int)count - prtn, 3 - frac_bits);
1593 prtn += snprintf(buffer + prtn, frac_bits, "%ld",
1594 frac_val * temp_mult / mult);
1597 while(buffer[prtn] < '1' || buffer[prtn] > '9') {
1599 if (buffer[prtn] == '.') {
1606 buffer[prtn++] ='\n';
1610 int lprocfs_seq_read_frac_helper(struct seq_file *m, long val, int mult)
1612 long decimal_val, frac_val;
1614 decimal_val = val / mult;
1615 seq_printf(m, "%ld", decimal_val);
1616 frac_val = val % mult;
1623 /* Three cases: x0, xx, 0x */
1624 if ((frac_val % 10) != 0)
1625 seq_printf(m, ".%ld", frac_val);
1627 seq_printf(m, ".%ld", frac_val / 10);
1630 seq_printf(m, "\n");
1633 EXPORT_SYMBOL(lprocfs_seq_read_frac_helper);
1635 /* Obtains the conversion factor for the unit specified */
1636 static int get_mult(char unit, __u64 *mult)
1641 /* peta, tera, giga, mega, and kilo */
1658 /* some tests expect % to be accepted */
1672 * Ensures the numeric string is valid. The function provides the final
1673 * multiplier in the case a unit exists at the end of the string. It also
1674 * locates the start of the whole and fractional parts (if any). This
1675 * function modifies the string so kstrtoull can be used to parse both
1676 * the whole and fraction portions. This function also figures out
1677 * the base of the number.
1679 static int preprocess_numeric_str(char *buffer, __u64 *mult, __u64 def_mult,
1680 bool allow_units, char **whole, char **frac,
1683 bool hit_decimal = false;
1684 bool hit_unit = false;
1692 /* a hex string if it starts with "0x" */
1693 if (buffer[0] == '0' && tolower(buffer[1]) == 'x') {
1701 /* allow for a single new line before the null terminator */
1702 if (*buffer == '\n') {
1712 /* any chars after our unit indicates a malformed string */
1716 /* ensure we only hit one decimal */
1717 if (*buffer == '.') {
1721 /* if past start, there's a whole part */
1722 if (start != buffer)
1728 } else if (!isdigit(*buffer) &&
1729 !(*base == 16 && isxdigit(*buffer))) {
1731 /* if we allow units, attempt to get mult */
1733 rc = get_mult(*buffer, mult);
1737 /* string stops here, but keep processing */
1749 /* hit a decimal, make sure there's a fractional part */
1755 /* didn't hit a decimal, but may have a whole part */
1756 if (start != buffer && *start)
1760 /* malformed string if we didn't get anything */
1761 if (!*frac && !*whole)
1768 * Parses a numeric string which can contain a whole and fraction portion
1769 * into a __u64. Accepts a multiplier to apply to the value parsed. Also
1770 * allows the string to have a unit at the end. The function handles
1771 * wrapping of the final unsigned value.
1773 static int str_to_u64_parse(char *buffer, unsigned long count,
1774 __u64 *val, __u64 def_mult, bool allow_units)
1778 unsigned int frac_d = 1;
1779 __u64 wrap_indicator = ULLONG_MAX;
1784 unsigned int base = 10;
1786 rc = preprocess_numeric_str(buffer, &mult, def_mult, allow_units,
1787 &strwhole, &strfrac, &base);
1797 /* the multiplier limits how large the value can be */
1798 wrap_indicator /= mult;
1801 rc = kstrtoull(strwhole, base, &whole);
1805 if (whole > wrap_indicator)
1812 if (strlen(strfrac) > 10)
1815 rc = kstrtoull(strfrac, base, &frac);
1819 /* determine power of fractional portion */
1825 /* fractional portion is too large to perform calculation */
1826 if (frac > wrap_indicator)
1830 do_div(frac, frac_d);
1833 /* check that the sum of whole and fraction fits in u64 */
1834 if (whole > (ULLONG_MAX - frac))
1837 *val = whole + frac;
1843 * This function parses numeric/hex strings into __s64. It accepts a multiplier
1844 * which will apply to the value parsed. It also can allow the string to
1845 * have a unit as the last character. The function handles overflow/underflow
1846 * of the signed integer.
1848 static int str_to_s64_internal(const char __user *buffer, unsigned long count,
1849 __s64 *val, __u64 def_mult, bool allow_units)
1853 unsigned int offset = 0;
1854 int signed sign = 1;
1855 __u64 max = LLONG_MAX;
1858 if (count > (sizeof(kernbuf) - 1))
1861 if (copy_from_user(kernbuf, buffer, count))
1864 kernbuf[count] = '\0';
1866 /* keep track of our sign */
1867 if (*kernbuf == '-') {
1870 /* equivalent to max = -LLONG_MIN, avoids overflow */
1874 rc = str_to_u64_parse(kernbuf + offset, count - offset,
1875 &tmp, def_mult, allow_units);
1879 /* check for overflow/underflow */
1883 *val = (__s64)tmp * sign;
1889 * Convert a user string into a signed 64 bit number. This function produces
1890 * an error when the value parsed from the string underflows or
1891 * overflows. This function accepts strings which contain digits and
1892 * optionally a decimal or hex strings which are prefixed with "0x".
1894 * \param[in] buffer string consisting of numbers and optionally a decimal
1895 * \param[in] count buffer length
1896 * \param[in] val if successful, the value represented by the string
1898 * \retval 0 on success
1899 * \retval negative number on error
1901 int lprocfs_str_to_s64(const char __user *buffer, unsigned long count,
1904 return str_to_s64_internal(buffer, count, val, 1, false);
1906 EXPORT_SYMBOL(lprocfs_str_to_s64);
1909 * Convert a user string into a signed 64 bit number. This function produces
1910 * an error when the value parsed from the string times multiplier underflows or
1911 * overflows. This function only accepts strings that contains digits, an
1912 * optional decimal, and a char representing a unit at the end. If a unit is
1913 * specified in the string, the multiplier provided by the caller is ignored.
1914 * This function can also accept hexadecimal strings which are prefixed with
1917 * \param[in] buffer string consisting of numbers, a decimal, and a unit
1918 * \param[in] count buffer length
1919 * \param[in] val if successful, the value represented by the string
1920 * \param[in] defunit default unit if string doesn't contain one
1922 * \retval 0 on success
1923 * \retval negative number on error
1925 int lprocfs_str_with_units_to_s64(const char __user *buffer,
1926 unsigned long count, __s64 *val, char defunit)
1931 rc = get_mult(defunit, &mult);
1935 return str_to_s64_internal(buffer, count, val, mult, true);
1937 EXPORT_SYMBOL(lprocfs_str_with_units_to_s64);
1939 static char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1948 if (!memcmp(s1, s2, l2))
1956 * Find the string \a name in the input \a buffer, and return a pointer to the
1957 * value immediately following \a name, reducing \a count appropriately.
1958 * If \a name is not found the original \a buffer is returned.
1960 char *lprocfs_find_named_value(const char *buffer, const char *name,
1964 size_t buflen = *count;
1966 /* there is no strnstr() in rhel5 and ubuntu kernels */
1967 val = lprocfs_strnstr(buffer, name, buflen);
1969 return (char *)buffer;
1971 val += strlen(name); /* skip prefix */
1972 while (val < buffer + buflen && isspace(*val)) /* skip separator */
1976 while (val < buffer + buflen && isalnum(*val)) {
1981 return val - *count;
1983 EXPORT_SYMBOL(lprocfs_find_named_value);
1985 int lprocfs_seq_create(struct proc_dir_entry *parent,
1988 const struct file_operations *seq_fops,
1991 struct proc_dir_entry *entry;
1994 /* Disallow secretly (un)writable entries. */
1995 LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
1997 entry = proc_create_data(name, mode, parent, seq_fops, data);
2004 EXPORT_SYMBOL(lprocfs_seq_create);
2006 int lprocfs_obd_seq_create(struct obd_device *dev,
2009 const struct file_operations *seq_fops,
2012 return (lprocfs_seq_create(dev->obd_proc_entry, name,
2013 mode, seq_fops, data));
2015 EXPORT_SYMBOL(lprocfs_obd_seq_create);
2017 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
2019 if (value >= OBD_HIST_MAX)
2020 value = OBD_HIST_MAX - 1;
2022 spin_lock(&oh->oh_lock);
2023 oh->oh_buckets[value]++;
2024 spin_unlock(&oh->oh_lock);
2026 EXPORT_SYMBOL(lprocfs_oh_tally);
2028 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
2030 unsigned int val = 0;
2032 if (likely(value != 0))
2033 val = min(fls(value - 1), OBD_HIST_MAX);
2035 lprocfs_oh_tally(oh, val);
2037 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
2039 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
2041 unsigned long ret = 0;
2044 for (i = 0; i < OBD_HIST_MAX; i++)
2045 ret += oh->oh_buckets[i];
2048 EXPORT_SYMBOL(lprocfs_oh_sum);
2050 void lprocfs_oh_clear(struct obd_histogram *oh)
2052 spin_lock(&oh->oh_lock);
2053 memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
2054 spin_unlock(&oh->oh_lock);
2056 EXPORT_SYMBOL(lprocfs_oh_clear);
2058 int lprocfs_obd_rd_max_pages_per_rpc(char *page, char **start, off_t off,
2059 int count, int *eof, void *data)
2061 struct obd_device *dev = data;
2062 struct client_obd *cli = &dev->u.cli;
2065 spin_lock(&cli->cl_loi_list_lock);
2066 rc = snprintf(page, count, "%d\n", cli->cl_max_pages_per_rpc);
2067 spin_unlock(&cli->cl_loi_list_lock);
2072 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
2074 struct obd_device *dev = data;
2075 struct client_obd *cli = &dev->u.cli;
2077 spin_lock(&cli->cl_loi_list_lock);
2078 seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
2079 spin_unlock(&cli->cl_loi_list_lock);
2082 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
2084 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2085 struct root_squash_info *squash, char *name)
2088 char kernbuf[64], *tmp, *errmsg;
2089 unsigned long uid, gid;
2092 if (count >= sizeof(kernbuf)) {
2093 errmsg = "string too long";
2094 GOTO(failed_noprint, rc = -EINVAL);
2096 if (copy_from_user(kernbuf, buffer, count)) {
2097 errmsg = "bad address";
2098 GOTO(failed_noprint, rc = -EFAULT);
2100 kernbuf[count] = '\0';
2102 /* look for uid gid separator */
2103 tmp = strchr(kernbuf, ':');
2105 errmsg = "needs uid:gid format";
2106 GOTO(failed, rc = -EINVAL);
2112 if (kstrtoul(kernbuf, 0, &uid) != 0) {
2114 GOTO(failed, rc = -EINVAL);
2118 if (kstrtoul(tmp, 0, &gid) != 0) {
2120 GOTO(failed, rc = -EINVAL);
2123 squash->rsi_uid = uid;
2124 squash->rsi_gid = gid;
2126 LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2127 name, squash->rsi_uid, squash->rsi_gid);
2135 CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2136 name, kernbuf, errmsg, rc);
2139 CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2143 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2146 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2147 struct root_squash_info *squash, char *name)
2150 char *kernbuf = NULL;
2152 struct list_head tmp;
2157 errmsg = "string too long";
2158 GOTO(failed, rc = -EINVAL);
2161 OBD_ALLOC(kernbuf, count + 1);
2162 if (kernbuf == NULL) {
2163 errmsg = "no memory";
2164 GOTO(failed, rc = -ENOMEM);
2166 if (copy_from_user(kernbuf, buffer, count)) {
2167 errmsg = "bad address";
2168 GOTO(failed, rc = -EFAULT);
2170 kernbuf[count] = '\0';
2172 if (count > 0 && kernbuf[count - 1] == '\n')
2175 if ((len == 4 && strncmp(kernbuf, "NONE", len) == 0) ||
2176 (len == 5 && strncmp(kernbuf, "clear", len) == 0)) {
2177 /* empty string is special case */
2178 down_write(&squash->rsi_sem);
2179 if (!list_empty(&squash->rsi_nosquash_nids))
2180 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2181 up_write(&squash->rsi_sem);
2182 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2183 OBD_FREE(kernbuf, count + 1);
2187 INIT_LIST_HEAD(&tmp);
2188 if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2189 errmsg = "can't parse";
2190 GOTO(failed, rc = -EINVAL);
2192 LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2194 OBD_FREE(kernbuf, count + 1);
2197 down_write(&squash->rsi_sem);
2198 if (!list_empty(&squash->rsi_nosquash_nids))
2199 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2200 list_splice(&tmp, &squash->rsi_nosquash_nids);
2201 up_write(&squash->rsi_sem);
2207 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2208 name, kernbuf, errmsg, rc);
2209 OBD_FREE(kernbuf, count + 1);
2211 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2216 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2218 #endif /* CONFIG_PROC_FS*/