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;
1278 do_gettimeofday(&now);
1279 seq_printf(p, "%-25s %lu.%lu secs.usecs\n",
1280 "snapshot_time", now.tv_sec, now.tv_usec);
1285 hdr = &stats->ls_cnt_header[idx];
1286 lprocfs_stats_collect(stats, idx, &ctr);
1288 if (ctr.lc_count == 0)
1291 seq_printf(p, "%-25s "LPD64" samples [%s]", hdr->lc_name,
1292 ctr.lc_count, hdr->lc_units);
1296 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1297 seq_printf(p, " "LPD64" "LPD64" "LPD64,
1298 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1301 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1302 seq_printf(p, " "LPD64, ctr.lc_sumsquare);
1308 return (rc < 0) ? rc : 0;
1311 static const struct seq_operations lprocfs_stats_seq_sops = {
1312 .start = lprocfs_stats_seq_start,
1313 .stop = lprocfs_stats_seq_stop,
1314 .next = lprocfs_stats_seq_next,
1315 .show = lprocfs_stats_seq_show,
1318 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1320 struct seq_file *seq;
1323 rc = LPROCFS_ENTRY_CHECK(inode);
1327 rc = seq_open(file, &lprocfs_stats_seq_sops);
1330 seq = file->private_data;
1331 seq->private = PDE_DATA(inode);
1335 static const struct file_operations lprocfs_stats_seq_fops = {
1336 .owner = THIS_MODULE,
1337 .open = lprocfs_stats_seq_open,
1339 .write = lprocfs_stats_seq_write,
1340 .llseek = seq_lseek,
1341 .release = lprocfs_seq_release,
1344 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1345 struct lprocfs_stats *stats)
1347 struct proc_dir_entry *entry;
1348 LASSERT(root != NULL);
1350 entry = proc_create_data(name, 0644, root,
1351 &lprocfs_stats_seq_fops, stats);
1356 EXPORT_SYMBOL(lprocfs_register_stats);
1358 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1359 unsigned conf, const char *name, const char *units)
1361 struct lprocfs_counter_header *header;
1362 struct lprocfs_counter *percpu_cntr;
1363 unsigned long flags = 0;
1365 unsigned int num_cpu;
1367 LASSERT(stats != NULL);
1369 header = &stats->ls_cnt_header[index];
1370 LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1371 index, name, units);
1373 header->lc_config = conf;
1374 header->lc_name = name;
1375 header->lc_units = units;
1377 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1378 for (i = 0; i < num_cpu; ++i) {
1379 if (stats->ls_percpu[i] == NULL)
1381 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1382 percpu_cntr->lc_count = 0;
1383 percpu_cntr->lc_min = LC_MIN_INIT;
1384 percpu_cntr->lc_max = 0;
1385 percpu_cntr->lc_sumsquare = 0;
1386 percpu_cntr->lc_sum = 0;
1387 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1388 percpu_cntr->lc_sum_irq = 0;
1390 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1392 EXPORT_SYMBOL(lprocfs_counter_init);
1394 /* Note that we only init md counters for ops whose offset is less
1395 * than NUM_MD_STATS. This is explained in a comment in the definition
1396 * of struct md_ops. */
1397 #define LPROCFS_MD_OP_INIT(base, stats, op) \
1399 unsigned int _idx = base + MD_COUNTER_OFFSET(op); \
1401 if (MD_COUNTER_OFFSET(op) < NUM_MD_STATS) { \
1402 LASSERT(_idx < stats->ls_num); \
1403 lprocfs_counter_init(stats, _idx, 0, #op, "reqs"); \
1407 void lprocfs_init_mps_stats(int num_private_stats, struct lprocfs_stats *stats)
1409 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_root);
1410 LPROCFS_MD_OP_INIT(num_private_stats, stats, null_inode);
1411 LPROCFS_MD_OP_INIT(num_private_stats, stats, close);
1412 LPROCFS_MD_OP_INIT(num_private_stats, stats, create);
1413 LPROCFS_MD_OP_INIT(num_private_stats, stats, enqueue);
1414 LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr);
1415 LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr_name);
1416 LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_lock);
1417 LPROCFS_MD_OP_INIT(num_private_stats, stats, link);
1418 LPROCFS_MD_OP_INIT(num_private_stats, stats, rename);
1419 LPROCFS_MD_OP_INIT(num_private_stats, stats, setattr);
1420 LPROCFS_MD_OP_INIT(num_private_stats, stats, fsync);
1421 LPROCFS_MD_OP_INIT(num_private_stats, stats, read_page);
1422 LPROCFS_MD_OP_INIT(num_private_stats, stats, unlink);
1423 LPROCFS_MD_OP_INIT(num_private_stats, stats, setxattr);
1424 LPROCFS_MD_OP_INIT(num_private_stats, stats, getxattr);
1425 LPROCFS_MD_OP_INIT(num_private_stats, stats, init_ea_size);
1426 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_lustre_md);
1427 LPROCFS_MD_OP_INIT(num_private_stats, stats, free_lustre_md);
1428 LPROCFS_MD_OP_INIT(num_private_stats, stats, merge_attr);
1429 LPROCFS_MD_OP_INIT(num_private_stats, stats, set_open_replay_data);
1430 LPROCFS_MD_OP_INIT(num_private_stats, stats, clear_open_replay_data);
1431 LPROCFS_MD_OP_INIT(num_private_stats, stats, set_lock_data);
1432 LPROCFS_MD_OP_INIT(num_private_stats, stats, lock_match);
1433 LPROCFS_MD_OP_INIT(num_private_stats, stats, cancel_unused);
1434 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_remote_perm);
1435 LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_getattr_async);
1436 LPROCFS_MD_OP_INIT(num_private_stats, stats, revalidate_lock);
1439 int lprocfs_alloc_md_stats(struct obd_device *obd,
1440 unsigned int num_private_stats)
1442 struct lprocfs_stats *stats;
1443 unsigned int num_stats;
1446 CLASSERT(offsetof(struct md_ops, MD_STATS_FIRST_OP) == 0);
1447 CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_FIRST_OP) == 0);
1448 CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_LAST_OP) > 0);
1450 /* TODO Ensure that this function is only used where
1451 * appropriate by adding an assertion to the effect that
1452 * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1453 * because mdt_procfs_init() uses this function to allocate
1454 * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1455 * mdt layer does not use the md_ops interface. This is
1456 * confusing and a waste of memory. See LU-2484.
1458 LASSERT(obd->obd_proc_entry != NULL);
1459 LASSERT(obd->obd_md_stats == NULL);
1460 LASSERT(obd->obd_md_cntr_base == 0);
1462 num_stats = NUM_MD_STATS + num_private_stats;
1463 stats = lprocfs_alloc_stats(num_stats, 0);
1467 lprocfs_init_mps_stats(num_private_stats, stats);
1469 for (i = num_private_stats; i < num_stats; i++) {
1470 if (stats->ls_cnt_header[i].lc_name == NULL) {
1471 CERROR("Missing md_stat initializer md_op "
1472 "operation at offset %d. Aborting.\n",
1473 i - num_private_stats);
1478 rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1480 lprocfs_free_stats(&stats);
1482 obd->obd_md_stats = stats;
1483 obd->obd_md_cntr_base = num_private_stats;
1488 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1490 void lprocfs_free_md_stats(struct obd_device *obd)
1492 struct lprocfs_stats *stats = obd->obd_md_stats;
1494 if (stats != NULL) {
1495 obd->obd_md_stats = NULL;
1496 obd->obd_md_cntr_base = 0;
1497 lprocfs_free_stats(&stats);
1500 EXPORT_SYMBOL(lprocfs_free_md_stats);
1502 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1504 lprocfs_counter_init(ldlm_stats,
1505 LDLM_ENQUEUE - LDLM_FIRST_OPC,
1506 0, "ldlm_enqueue", "reqs");
1507 lprocfs_counter_init(ldlm_stats,
1508 LDLM_CONVERT - LDLM_FIRST_OPC,
1509 0, "ldlm_convert", "reqs");
1510 lprocfs_counter_init(ldlm_stats,
1511 LDLM_CANCEL - LDLM_FIRST_OPC,
1512 0, "ldlm_cancel", "reqs");
1513 lprocfs_counter_init(ldlm_stats,
1514 LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1515 0, "ldlm_bl_callback", "reqs");
1516 lprocfs_counter_init(ldlm_stats,
1517 LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1518 0, "ldlm_cp_callback", "reqs");
1519 lprocfs_counter_init(ldlm_stats,
1520 LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1521 0, "ldlm_gl_callback", "reqs");
1523 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1525 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1526 struct lprocfs_counter_header *header,
1527 enum lprocfs_stats_flags flags,
1528 enum lprocfs_fields_flags field)
1532 if (lc == NULL || header == NULL)
1536 case LPROCFS_FIELDS_FLAGS_CONFIG:
1537 ret = header->lc_config;
1539 case LPROCFS_FIELDS_FLAGS_SUM:
1541 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1542 ret += lc->lc_sum_irq;
1544 case LPROCFS_FIELDS_FLAGS_MIN:
1547 case LPROCFS_FIELDS_FLAGS_MAX:
1550 case LPROCFS_FIELDS_FLAGS_AVG:
1551 ret = (lc->lc_max - lc->lc_min) / 2;
1553 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1554 ret = lc->lc_sumsquare;
1556 case LPROCFS_FIELDS_FLAGS_COUNT:
1564 EXPORT_SYMBOL(lprocfs_read_helper);
1566 int lprocfs_read_frac_helper(char *buffer, unsigned long count, long val,
1569 long decimal_val, frac_val;
1575 decimal_val = val / mult;
1576 prtn = snprintf(buffer, count, "%ld", decimal_val);
1577 frac_val = val % mult;
1579 if (prtn < (count - 4) && frac_val > 0) {
1581 int i, temp_mult = 1, frac_bits = 0;
1583 temp_frac = frac_val * 10;
1584 buffer[prtn++] = '.';
1585 while (frac_bits < 2 && (temp_frac / mult) < 1 ) {
1586 /* only reserved 2 bits fraction */
1587 buffer[prtn++] ='0';
1592 * Need to think these cases :
1593 * 1. #echo x.00 > /proc/xxx output result : x
1594 * 2. #echo x.0x > /proc/xxx output result : x.0x
1595 * 3. #echo x.x0 > /proc/xxx output result : x.x
1596 * 4. #echo x.xx > /proc/xxx output result : x.xx
1597 * Only reserved 2 bits fraction.
1599 for (i = 0; i < (5 - prtn); i++)
1602 frac_bits = min((int)count - prtn, 3 - frac_bits);
1603 prtn += snprintf(buffer + prtn, frac_bits, "%ld",
1604 frac_val * temp_mult / mult);
1607 while(buffer[prtn] < '1' || buffer[prtn] > '9') {
1609 if (buffer[prtn] == '.') {
1616 buffer[prtn++] ='\n';
1620 int lprocfs_seq_read_frac_helper(struct seq_file *m, long val, int mult)
1622 long decimal_val, frac_val;
1624 decimal_val = val / mult;
1625 seq_printf(m, "%ld", decimal_val);
1626 frac_val = val % mult;
1633 /* Three cases: x0, xx, 0x */
1634 if ((frac_val % 10) != 0)
1635 seq_printf(m, ".%ld", frac_val);
1637 seq_printf(m, ".%ld", frac_val / 10);
1640 seq_printf(m, "\n");
1643 EXPORT_SYMBOL(lprocfs_seq_read_frac_helper);
1645 /* Obtains the conversion factor for the unit specified */
1646 static int get_mult(char unit, __u64 *mult)
1651 /* peta, tera, giga, mega, and kilo */
1668 /* some tests expect % to be accepted */
1682 * Ensures the numeric string is valid. The function provides the final
1683 * multiplier in the case a unit exists at the end of the string. It also
1684 * locates the start of the whole and fractional parts (if any). This
1685 * function modifies the string so kstrtoull can be used to parse both
1686 * the whole and fraction portions. This function also figures out
1687 * the base of the number.
1689 static int preprocess_numeric_str(char *buffer, __u64 *mult, __u64 def_mult,
1690 bool allow_units, char **whole, char **frac,
1693 bool hit_decimal = false;
1694 bool hit_unit = false;
1702 /* a hex string if it starts with "0x" */
1703 if (buffer[0] == '0' && tolower(buffer[1]) == 'x') {
1711 /* allow for a single new line before the null terminator */
1712 if (*buffer == '\n') {
1722 /* any chars after our unit indicates a malformed string */
1726 /* ensure we only hit one decimal */
1727 if (*buffer == '.') {
1731 /* if past start, there's a whole part */
1732 if (start != buffer)
1738 } else if (!isdigit(*buffer) &&
1739 !(*base == 16 && isxdigit(*buffer))) {
1741 /* if we allow units, attempt to get mult */
1743 rc = get_mult(*buffer, mult);
1747 /* string stops here, but keep processing */
1759 /* hit a decimal, make sure there's a fractional part */
1765 /* didn't hit a decimal, but may have a whole part */
1766 if (start != buffer && *start)
1770 /* malformed string if we didn't get anything */
1771 if (!*frac && !*whole)
1778 * Parses a numeric string which can contain a whole and fraction portion
1779 * into a __u64. Accepts a multiplier to apply to the value parsed. Also
1780 * allows the string to have a unit at the end. The function handles
1781 * wrapping of the final unsigned value.
1783 static int str_to_u64_parse(char *buffer, unsigned long count,
1784 __u64 *val, __u64 def_mult, bool allow_units)
1788 unsigned int frac_d = 1;
1789 __u64 wrap_indicator = ULLONG_MAX;
1794 unsigned int base = 10;
1796 rc = preprocess_numeric_str(buffer, &mult, def_mult, allow_units,
1797 &strwhole, &strfrac, &base);
1807 /* the multiplier limits how large the value can be */
1808 wrap_indicator /= mult;
1811 rc = kstrtoull(strwhole, base, &whole);
1815 if (whole > wrap_indicator)
1822 if (strlen(strfrac) > 10)
1825 rc = kstrtoull(strfrac, base, &frac);
1829 /* determine power of fractional portion */
1835 /* fractional portion is too large to perform calculation */
1836 if (frac > wrap_indicator)
1840 do_div(frac, frac_d);
1843 /* check that the sum of whole and fraction fits in u64 */
1844 if (whole > (ULLONG_MAX - frac))
1847 *val = whole + frac;
1853 * This function parses numeric/hex strings into __s64. It accepts a multiplier
1854 * which will apply to the value parsed. It also can allow the string to
1855 * have a unit as the last character. The function handles overflow/underflow
1856 * of the signed integer.
1858 static int str_to_s64_internal(const char __user *buffer, unsigned long count,
1859 __s64 *val, __u64 def_mult, bool allow_units)
1863 unsigned int offset = 0;
1864 int signed sign = 1;
1865 __u64 max = LLONG_MAX;
1868 if (count > (sizeof(kernbuf) - 1))
1871 if (copy_from_user(kernbuf, buffer, count))
1874 kernbuf[count] = '\0';
1876 /* keep track of our sign */
1877 if (*kernbuf == '-') {
1880 /* equivalent to max = -LLONG_MIN, avoids overflow */
1884 rc = str_to_u64_parse(kernbuf + offset, count - offset,
1885 &tmp, def_mult, allow_units);
1889 /* check for overflow/underflow */
1893 *val = (__s64)tmp * sign;
1899 * Convert a user string into a signed 64 bit number. This function produces
1900 * an error when the value parsed from the string underflows or
1901 * overflows. This function accepts strings which contain digits and
1902 * optionally a decimal or hex strings which are prefixed with "0x".
1904 * \param[in] buffer string consisting of numbers and optionally a decimal
1905 * \param[in] count buffer length
1906 * \param[in] val if successful, the value represented by the string
1908 * \retval 0 on success
1909 * \retval negative number on error
1911 int lprocfs_str_to_s64(const char __user *buffer, unsigned long count,
1914 return str_to_s64_internal(buffer, count, val, 1, false);
1916 EXPORT_SYMBOL(lprocfs_str_to_s64);
1919 * Convert a user string into a signed 64 bit number. This function produces
1920 * an error when the value parsed from the string times multiplier underflows or
1921 * overflows. This function only accepts strings that contains digits, an
1922 * optional decimal, and a char representing a unit at the end. If a unit is
1923 * specified in the string, the multiplier provided by the caller is ignored.
1924 * This function can also accept hexadecimal strings which are prefixed with
1927 * \param[in] buffer string consisting of numbers, a decimal, and a unit
1928 * \param[in] count buffer length
1929 * \param[in] val if successful, the value represented by the string
1930 * \param[in] defunit default unit if string doesn't contain one
1932 * \retval 0 on success
1933 * \retval negative number on error
1935 int lprocfs_str_with_units_to_s64(const char __user *buffer,
1936 unsigned long count, __s64 *val, char defunit)
1941 rc = get_mult(defunit, &mult);
1945 return str_to_s64_internal(buffer, count, val, mult, true);
1947 EXPORT_SYMBOL(lprocfs_str_with_units_to_s64);
1949 static char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1958 if (!memcmp(s1, s2, l2))
1966 * Find the string \a name in the input \a buffer, and return a pointer to the
1967 * value immediately following \a name, reducing \a count appropriately.
1968 * If \a name is not found the original \a buffer is returned.
1970 char *lprocfs_find_named_value(const char *buffer, const char *name,
1974 size_t buflen = *count;
1976 /* there is no strnstr() in rhel5 and ubuntu kernels */
1977 val = lprocfs_strnstr(buffer, name, buflen);
1979 return (char *)buffer;
1981 val += strlen(name); /* skip prefix */
1982 while (val < buffer + buflen && isspace(*val)) /* skip separator */
1986 while (val < buffer + buflen && isalnum(*val)) {
1991 return val - *count;
1993 EXPORT_SYMBOL(lprocfs_find_named_value);
1995 int lprocfs_seq_create(struct proc_dir_entry *parent,
1998 const struct file_operations *seq_fops,
2001 struct proc_dir_entry *entry;
2004 /* Disallow secretly (un)writable entries. */
2005 LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
2007 entry = proc_create_data(name, mode, parent, seq_fops, data);
2014 EXPORT_SYMBOL(lprocfs_seq_create);
2016 int lprocfs_obd_seq_create(struct obd_device *dev,
2019 const struct file_operations *seq_fops,
2022 return (lprocfs_seq_create(dev->obd_proc_entry, name,
2023 mode, seq_fops, data));
2025 EXPORT_SYMBOL(lprocfs_obd_seq_create);
2027 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
2029 if (value >= OBD_HIST_MAX)
2030 value = OBD_HIST_MAX - 1;
2032 spin_lock(&oh->oh_lock);
2033 oh->oh_buckets[value]++;
2034 spin_unlock(&oh->oh_lock);
2036 EXPORT_SYMBOL(lprocfs_oh_tally);
2038 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
2040 unsigned int val = 0;
2042 if (likely(value != 0))
2043 val = min(fls(value - 1), OBD_HIST_MAX);
2045 lprocfs_oh_tally(oh, val);
2047 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
2049 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
2051 unsigned long ret = 0;
2054 for (i = 0; i < OBD_HIST_MAX; i++)
2055 ret += oh->oh_buckets[i];
2058 EXPORT_SYMBOL(lprocfs_oh_sum);
2060 void lprocfs_oh_clear(struct obd_histogram *oh)
2062 spin_lock(&oh->oh_lock);
2063 memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
2064 spin_unlock(&oh->oh_lock);
2066 EXPORT_SYMBOL(lprocfs_oh_clear);
2068 int lprocfs_obd_rd_max_pages_per_rpc(char *page, char **start, off_t off,
2069 int count, int *eof, void *data)
2071 struct obd_device *dev = data;
2072 struct client_obd *cli = &dev->u.cli;
2075 spin_lock(&cli->cl_loi_list_lock);
2076 rc = snprintf(page, count, "%d\n", cli->cl_max_pages_per_rpc);
2077 spin_unlock(&cli->cl_loi_list_lock);
2082 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
2084 struct obd_device *dev = data;
2085 struct client_obd *cli = &dev->u.cli;
2087 spin_lock(&cli->cl_loi_list_lock);
2088 seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
2089 spin_unlock(&cli->cl_loi_list_lock);
2092 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
2094 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2095 struct root_squash_info *squash, char *name)
2098 char kernbuf[64], *tmp, *errmsg;
2099 unsigned long uid, gid;
2102 if (count >= sizeof(kernbuf)) {
2103 errmsg = "string too long";
2104 GOTO(failed_noprint, rc = -EINVAL);
2106 if (copy_from_user(kernbuf, buffer, count)) {
2107 errmsg = "bad address";
2108 GOTO(failed_noprint, rc = -EFAULT);
2110 kernbuf[count] = '\0';
2112 /* look for uid gid separator */
2113 tmp = strchr(kernbuf, ':');
2115 errmsg = "needs uid:gid format";
2116 GOTO(failed, rc = -EINVAL);
2122 if (kstrtoul(kernbuf, 0, &uid) != 0) {
2124 GOTO(failed, rc = -EINVAL);
2128 if (kstrtoul(tmp, 0, &gid) != 0) {
2130 GOTO(failed, rc = -EINVAL);
2133 squash->rsi_uid = uid;
2134 squash->rsi_gid = gid;
2136 LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2137 name, squash->rsi_uid, squash->rsi_gid);
2145 CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2146 name, kernbuf, errmsg, rc);
2149 CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2153 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2156 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2157 struct root_squash_info *squash, char *name)
2160 char *kernbuf = NULL;
2162 struct list_head tmp;
2167 errmsg = "string too long";
2168 GOTO(failed, rc = -EINVAL);
2171 OBD_ALLOC(kernbuf, count + 1);
2172 if (kernbuf == NULL) {
2173 errmsg = "no memory";
2174 GOTO(failed, rc = -ENOMEM);
2176 if (copy_from_user(kernbuf, buffer, count)) {
2177 errmsg = "bad address";
2178 GOTO(failed, rc = -EFAULT);
2180 kernbuf[count] = '\0';
2182 if (count > 0 && kernbuf[count - 1] == '\n')
2185 if ((len == 4 && strncmp(kernbuf, "NONE", len) == 0) ||
2186 (len == 5 && strncmp(kernbuf, "clear", len) == 0)) {
2187 /* empty string is special case */
2188 down_write(&squash->rsi_sem);
2189 if (!list_empty(&squash->rsi_nosquash_nids))
2190 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2191 up_write(&squash->rsi_sem);
2192 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2193 OBD_FREE(kernbuf, count + 1);
2197 INIT_LIST_HEAD(&tmp);
2198 if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2199 errmsg = "can't parse";
2200 GOTO(failed, rc = -EINVAL);
2202 LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2204 OBD_FREE(kernbuf, count + 1);
2207 down_write(&squash->rsi_sem);
2208 if (!list_empty(&squash->rsi_nosquash_nids))
2209 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2210 list_splice(&tmp, &squash->rsi_nosquash_nids);
2211 up_write(&squash->rsi_sem);
2217 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2218 name, kernbuf, errmsg, rc);
2219 OBD_FREE(kernbuf, count + 1);
2221 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2226 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2228 #endif /* CONFIG_PROC_FS*/