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, 2014, 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 CFS_MODULE_PARM(lprocfs_no_percpu_stats, "i", int, 0644,
51 "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 return seq_printf(m, "%u\n", *(unsigned int *)data);
297 EXPORT_SYMBOL(lprocfs_uint_seq_show);
299 int lprocfs_wr_uint(struct file *file, const char __user *buffer,
300 unsigned long count, void *data)
303 char dummy[MAX_STRING_SIZE + 1], *end;
306 dummy[MAX_STRING_SIZE] = '\0';
307 if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
310 tmp = simple_strtoul(dummy, &end, 0);
314 *p = (unsigned int)tmp;
317 EXPORT_SYMBOL(lprocfs_wr_uint);
319 ssize_t lprocfs_uint_seq_write(struct file *file, const char __user *buffer,
320 size_t count, loff_t *off)
322 int *data = ((struct seq_file *)file->private_data)->private;
325 rc = lprocfs_write_helper(buffer, count, &val);
329 return lprocfs_wr_uint(file, buffer, count, data);
331 EXPORT_SYMBOL(lprocfs_uint_seq_write);
333 int lprocfs_u64_seq_show(struct seq_file *m, void *data)
335 LASSERT(data != NULL);
336 return seq_printf(m, LPU64"\n", *(__u64 *)data);
338 EXPORT_SYMBOL(lprocfs_u64_seq_show);
340 int lprocfs_atomic_seq_show(struct seq_file *m, void *data)
342 atomic_t *atom = data;
343 LASSERT(atom != NULL);
344 return seq_printf(m, "%d\n", atomic_read(atom));
346 EXPORT_SYMBOL(lprocfs_atomic_seq_show);
349 lprocfs_atomic_seq_write(struct file *file, const char __user *buffer,
350 size_t count, loff_t *off)
352 atomic_t *atm = ((struct seq_file *)file->private_data)->private;
356 rc = lprocfs_write_helper(buffer, count, &val);
363 atomic_set(atm, val);
366 EXPORT_SYMBOL(lprocfs_atomic_seq_write);
368 int lprocfs_uuid_seq_show(struct seq_file *m, void *data)
370 struct obd_device *obd = data;
372 LASSERT(obd != NULL);
373 return seq_printf(m, "%s\n", obd->obd_uuid.uuid);
375 EXPORT_SYMBOL(lprocfs_uuid_seq_show);
377 int lprocfs_name_seq_show(struct seq_file *m, void *data)
379 struct obd_device *dev = data;
381 LASSERT(dev != NULL);
382 return seq_printf(m, "%s\n", dev->obd_name);
384 EXPORT_SYMBOL(lprocfs_name_seq_show);
386 int lprocfs_blksize_seq_show(struct seq_file *m, void *data)
388 struct obd_device *obd = data;
389 struct obd_statfs osfs;
390 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
391 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
394 rc = seq_printf(m, "%u\n", osfs.os_bsize);
397 EXPORT_SYMBOL(lprocfs_blksize_seq_show);
399 int lprocfs_kbytestotal_seq_show(struct seq_file *m, void *data)
401 struct obd_device *obd = data;
402 struct obd_statfs osfs;
403 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
404 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
407 __u32 blk_size = osfs.os_bsize >> 10;
408 __u64 result = osfs.os_blocks;
410 while (blk_size >>= 1)
413 rc = seq_printf(m, LPU64"\n", result);
417 EXPORT_SYMBOL(lprocfs_kbytestotal_seq_show);
419 int lprocfs_kbytesfree_seq_show(struct seq_file *m, void *data)
421 struct obd_device *obd = data;
422 struct obd_statfs osfs;
423 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
424 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
427 __u32 blk_size = osfs.os_bsize >> 10;
428 __u64 result = osfs.os_bfree;
430 while (blk_size >>= 1)
433 rc = seq_printf(m, LPU64"\n", result);
437 EXPORT_SYMBOL(lprocfs_kbytesfree_seq_show);
439 int lprocfs_kbytesavail_seq_show(struct seq_file *m, void *data)
441 struct obd_device *obd = data;
442 struct obd_statfs osfs;
443 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
444 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
447 __u32 blk_size = osfs.os_bsize >> 10;
448 __u64 result = osfs.os_bavail;
450 while (blk_size >>= 1)
453 rc = seq_printf(m, LPU64"\n", result);
457 EXPORT_SYMBOL(lprocfs_kbytesavail_seq_show);
459 int lprocfs_filestotal_seq_show(struct seq_file *m, void *data)
461 struct obd_device *obd = data;
462 struct obd_statfs osfs;
463 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
464 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
467 rc = seq_printf(m, LPU64"\n", osfs.os_files);
470 EXPORT_SYMBOL(lprocfs_filestotal_seq_show);
472 int lprocfs_filesfree_seq_show(struct seq_file *m, void *data)
474 struct obd_device *obd = data;
475 struct obd_statfs osfs;
476 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
477 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
480 rc = seq_printf(m, LPU64"\n", osfs.os_ffree);
483 EXPORT_SYMBOL(lprocfs_filesfree_seq_show);
485 int lprocfs_server_uuid_seq_show(struct seq_file *m, void *data)
487 struct obd_device *obd = data;
488 struct obd_import *imp;
489 char *imp_state_name = NULL;
492 LASSERT(obd != NULL);
493 LPROCFS_CLIMP_CHECK(obd);
494 imp = obd->u.cli.cl_import;
495 imp_state_name = ptlrpc_import_state_name(imp->imp_state);
496 rc = seq_printf(m, "%s\t%s%s\n", obd2cli_tgt(obd), imp_state_name,
497 imp->imp_deactive ? "\tDEACTIVATED" : "");
499 LPROCFS_CLIMP_EXIT(obd);
502 EXPORT_SYMBOL(lprocfs_server_uuid_seq_show);
504 int lprocfs_conn_uuid_seq_show(struct seq_file *m, void *data)
506 struct obd_device *obd = data;
507 struct ptlrpc_connection *conn;
510 LASSERT(obd != NULL);
512 LPROCFS_CLIMP_CHECK(obd);
513 conn = obd->u.cli.cl_import->imp_connection;
514 if (conn && obd->u.cli.cl_import)
515 rc = seq_printf(m, "%s\n", conn->c_remote_uuid.uuid);
517 rc = seq_printf(m, "%s\n", "<none>");
519 LPROCFS_CLIMP_EXIT(obd);
522 EXPORT_SYMBOL(lprocfs_conn_uuid_seq_show);
524 /** add up per-cpu counters */
525 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
526 struct lprocfs_counter *cnt)
528 unsigned int num_entry;
529 struct lprocfs_counter *percpu_cntr;
531 unsigned long flags = 0;
533 memset(cnt, 0, sizeof(*cnt));
536 /* set count to 1 to avoid divide-by-zero errs in callers */
541 cnt->lc_min = LC_MIN_INIT;
543 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
545 for (i = 0; i < num_entry; i++) {
546 if (stats->ls_percpu[i] == NULL)
548 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
550 cnt->lc_count += percpu_cntr->lc_count;
551 cnt->lc_sum += percpu_cntr->lc_sum;
552 if (percpu_cntr->lc_min < cnt->lc_min)
553 cnt->lc_min = percpu_cntr->lc_min;
554 if (percpu_cntr->lc_max > cnt->lc_max)
555 cnt->lc_max = percpu_cntr->lc_max;
556 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
559 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
563 * Append a space separated list of current set flags to str.
565 #define flag2str(flag) \
567 if (imp->imp_##flag) { \
568 seq_printf(m, "%s" #flag, first ? "" : ", "); \
572 static void obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
576 if (imp->imp_obd->obd_no_recov) {
577 seq_printf(m, "no_recov");
583 flag2str(replayable);
584 flag2str(delayed_recovery);
585 flag2str(no_lock_replay);
586 flag2str(vbr_failed);
588 flag2str(resend_replay);
589 flag2str(no_pinger_recover);
590 flag2str(need_mne_swab);
591 flag2str(connect_tried);
595 static const char *obd_connect_names[] = {
609 "join_file(obsolete)",
613 "remote_client_by_force",
622 "mds_mds_connection",
625 "alt_checksum_algorithm",
659 static void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, char *sep)
665 for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
667 seq_printf(m, "%s%s",
668 first ? "" : sep, obd_connect_names[i]);
672 if (flags & ~(mask - 1))
673 seq_printf(m, "%sunknown_"LPX64,
674 first ? "" : sep, flags & ~(mask - 1));
677 int obd_connect_flags2str(char *page, int count, __u64 flags, char *sep)
682 for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
684 ret += snprintf(page + ret, count - ret, "%s%s",
685 ret ? sep : "", obd_connect_names[i]);
687 if (flags & ~(mask - 1))
688 ret += snprintf(page + ret, count - ret,
690 ret ? sep : "", flags & ~(mask - 1));
693 EXPORT_SYMBOL(obd_connect_flags2str);
695 static void obd_connect_data_seqprint(struct seq_file *m,
696 struct obd_connect_data *ocd)
700 LASSERT(ocd != NULL);
701 flags = ocd->ocd_connect_flags;
703 seq_printf(m, " connect_data:\n"
706 ocd->ocd_connect_flags,
708 if (flags & OBD_CONNECT_VERSION)
709 seq_printf(m, " target_version: %u.%u.%u.%u\n",
710 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
711 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
712 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
713 OBD_OCD_VERSION_FIX(ocd->ocd_version));
714 if (flags & OBD_CONNECT_MDS)
715 seq_printf(m, " mdt_index: %d\n", ocd->ocd_group);
716 if (flags & OBD_CONNECT_GRANT)
717 seq_printf(m, " initial_grant: %d\n", ocd->ocd_grant);
718 if (flags & OBD_CONNECT_INDEX)
719 seq_printf(m, " target_index: %u\n", ocd->ocd_index);
720 if (flags & OBD_CONNECT_BRW_SIZE)
721 seq_printf(m, " max_brw_size: %d\n", ocd->ocd_brw_size);
722 if (flags & OBD_CONNECT_IBITS)
723 seq_printf(m, " ibits_known: "LPX64"\n",
724 ocd->ocd_ibits_known);
725 if (flags & OBD_CONNECT_GRANT_PARAM)
726 seq_printf(m, " grant_block_size: %d\n"
727 " grant_inode_size: %d\n"
728 " grant_extent_overhead: %d\n",
731 ocd->ocd_grant_extent);
732 if (flags & OBD_CONNECT_TRANSNO)
733 seq_printf(m, " first_transno: "LPX64"\n",
735 if (flags & OBD_CONNECT_CKSUM)
736 seq_printf(m, " cksum_types: %#x\n",
737 ocd->ocd_cksum_types);
738 if (flags & OBD_CONNECT_MAX_EASIZE)
739 seq_printf(m, " max_easize: %d\n", ocd->ocd_max_easize);
740 if (flags & OBD_CONNECT_MAXBYTES)
741 seq_printf(m, " max_object_bytes: "LPU64"\n",
745 int lprocfs_import_seq_show(struct seq_file *m, void *data)
747 struct lprocfs_counter ret;
748 struct lprocfs_counter_header *header;
749 struct obd_device *obd = (struct obd_device *)data;
750 struct obd_import *imp;
751 struct obd_import_conn *conn;
752 struct obd_connect_data *ocd;
757 LASSERT(obd != NULL);
758 LPROCFS_CLIMP_CHECK(obd);
759 imp = obd->u.cli.cl_import;
760 ocd = &imp->imp_connect_data;
762 seq_printf(m, "import:\n"
766 " connect_flags: [ ",
769 ptlrpc_import_state_name(imp->imp_state));
770 obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
772 seq_printf(m, " ]\n");
773 obd_connect_data_seqprint(m, ocd);
774 seq_printf(m, " import_flags: [ ");
775 obd_import_flags2str(imp, m);
779 " failover_nids: [ ");
780 spin_lock(&imp->imp_lock);
782 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
783 seq_printf(m, "%s%s", j ? ", " : "",
784 libcfs_nid2str(conn->oic_conn->c_peer.nid));
788 " current_connection: %s\n"
789 " connection_attempts: %u\n"
791 " in-progress_invalidations: %u\n",
792 imp->imp_connection == NULL ? "<none>" :
793 libcfs_nid2str(imp->imp_connection->c_peer.nid),
796 atomic_read(&imp->imp_inval_count));
797 spin_unlock(&imp->imp_lock);
799 if (obd->obd_svc_stats == NULL)
802 header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
803 lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
804 if (ret.lc_count != 0) {
805 /* first argument to do_div MUST be __u64 */
806 __u64 sum = ret.lc_sum;
807 do_div(sum, ret.lc_count);
811 seq_printf(m, " rpcs:\n"
813 " unregistering: %u\n"
815 " avg_waittime: "LPU64" %s\n",
816 atomic_read(&imp->imp_inflight),
817 atomic_read(&imp->imp_unregistering),
818 atomic_read(&imp->imp_timeouts),
819 ret.lc_sum, header->lc_units);
822 for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
823 if (imp->imp_at.iat_portal[j] == 0)
825 k = max_t(unsigned int, k,
826 at_get(&imp->imp_at.iat_service_estimate[j]));
828 seq_printf(m, " service_estimates:\n"
829 " services: %u sec\n"
830 " network: %u sec\n",
832 at_get(&imp->imp_at.iat_net_latency));
834 seq_printf(m, " transactions:\n"
835 " last_replay: "LPU64"\n"
836 " peer_committed: "LPU64"\n"
837 " last_checked: "LPU64"\n",
838 imp->imp_last_replay_transno,
839 imp->imp_peer_committed_transno,
840 imp->imp_last_transno_checked);
843 for (rw = 0; rw <= 1; rw++) {
844 lprocfs_stats_collect(obd->obd_svc_stats,
845 PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
847 if (ret.lc_sum > 0 && ret.lc_count > 0) {
848 /* first argument to do_div MUST be __u64 */
849 __u64 sum = ret.lc_sum;
850 do_div(sum, ret.lc_count);
852 seq_printf(m, " %s_data_averages:\n"
853 " bytes_per_rpc: "LPU64"\n",
854 rw ? "write" : "read",
858 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
859 header = &obd->obd_svc_stats->ls_cnt_header[j];
860 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
861 if (ret.lc_sum > 0 && ret.lc_count != 0) {
862 /* first argument to do_div MUST be __u64 */
863 __u64 sum = ret.lc_sum;
864 do_div(sum, ret.lc_count);
866 seq_printf(m, " %s_per_rpc: "LPU64"\n",
867 header->lc_units, ret.lc_sum);
870 seq_printf(m, " MB_per_sec: %u.%.02u\n",
871 k / j, (100 * k / j) % 100);
876 LPROCFS_CLIMP_EXIT(obd);
879 EXPORT_SYMBOL(lprocfs_import_seq_show);
881 int lprocfs_state_seq_show(struct seq_file *m, void *data)
883 struct obd_device *obd = (struct obd_device *)data;
884 struct obd_import *imp;
887 LASSERT(obd != NULL);
888 LPROCFS_CLIMP_CHECK(obd);
889 imp = obd->u.cli.cl_import;
891 seq_printf(m, "current_state: %s\n",
892 ptlrpc_import_state_name(imp->imp_state));
893 seq_printf(m, "state_history:\n");
894 k = imp->imp_state_hist_idx;
895 for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
896 struct import_state_hist *ish =
897 &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
898 if (ish->ish_state == 0)
900 seq_printf(m, " - [ "CFS_TIME_T", %s ]\n",
902 ptlrpc_import_state_name(ish->ish_state));
905 LPROCFS_CLIMP_EXIT(obd);
908 EXPORT_SYMBOL(lprocfs_state_seq_show);
910 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
913 for (i = 0; i < AT_BINS; i++)
914 seq_printf(m, "%3u ", at->at_hist[i]);
918 EXPORT_SYMBOL(lprocfs_at_hist_helper);
920 /* See also ptlrpc_lprocfs_timeouts_show_seq */
921 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
923 struct obd_device *obd = (struct obd_device *)data;
924 struct obd_import *imp;
925 unsigned int cur, worst;
930 LASSERT(obd != NULL);
931 LPROCFS_CLIMP_CHECK(obd);
932 imp = obd->u.cli.cl_import;
934 now = cfs_time_current_sec();
936 /* Some network health info for kicks */
937 s2dhms(&ts, now - imp->imp_last_reply_time);
938 seq_printf(m, "%-10s : %ld, "DHMS_FMT" ago\n",
939 "last reply", imp->imp_last_reply_time, DHMS_VARS(&ts));
941 cur = at_get(&imp->imp_at.iat_net_latency);
942 worst = imp->imp_at.iat_net_latency.at_worst_ever;
943 worstt = imp->imp_at.iat_net_latency.at_worst_time;
944 s2dhms(&ts, now - worstt);
945 seq_printf(m, "%-10s : cur %3u worst %3u (at %ld, "DHMS_FMT" ago) ",
946 "network", cur, worst, worstt, DHMS_VARS(&ts));
947 lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
949 for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
950 if (imp->imp_at.iat_portal[i] == 0)
952 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
953 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
954 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
955 s2dhms(&ts, now - worstt);
956 seq_printf(m, "portal %-2d : cur %3u worst %3u (at %ld, "
957 DHMS_FMT" ago) ", imp->imp_at.iat_portal[i],
958 cur, worst, worstt, DHMS_VARS(&ts));
959 lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
962 LPROCFS_CLIMP_EXIT(obd);
965 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
967 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
969 struct obd_device *obd = data;
972 LPROCFS_CLIMP_CHECK(obd);
973 flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
974 seq_printf(m, "flags="LPX64"\n", flags);
975 obd_connect_seq_flags2str(m, flags, "\n");
977 LPROCFS_CLIMP_EXIT(obd);
980 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
983 lprocfs_obd_setup(struct obd_device *obd)
987 LASSERT(obd != NULL);
988 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
989 LASSERT(obd->obd_type->typ_procroot != NULL);
991 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
992 obd->obd_type->typ_procroot,
994 if (IS_ERR(obd->obd_proc_entry)) {
995 rc = PTR_ERR(obd->obd_proc_entry);
996 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
997 obd->obd_proc_entry = NULL;
1001 EXPORT_SYMBOL(lprocfs_obd_setup);
1003 int lprocfs_obd_cleanup(struct obd_device *obd)
1007 if (obd->obd_proc_exports_entry) {
1008 /* Should be no exports left */
1009 lprocfs_remove(&obd->obd_proc_exports_entry);
1010 obd->obd_proc_exports_entry = NULL;
1012 if (obd->obd_proc_entry) {
1013 lprocfs_remove(&obd->obd_proc_entry);
1014 obd->obd_proc_entry = NULL;
1018 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1020 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1022 struct lprocfs_counter *cntr;
1023 unsigned int percpusize;
1025 unsigned long flags = 0;
1028 LASSERT(stats->ls_percpu[cpuid] == NULL);
1029 LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1031 percpusize = lprocfs_stats_counter_size(stats);
1032 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1033 if (stats->ls_percpu[cpuid] != NULL) {
1035 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1036 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1037 spin_lock_irqsave(&stats->ls_lock, flags);
1039 spin_lock(&stats->ls_lock);
1040 if (stats->ls_biggest_alloc_num <= cpuid)
1041 stats->ls_biggest_alloc_num = cpuid + 1;
1042 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1043 spin_unlock_irqrestore(&stats->ls_lock, flags);
1045 spin_unlock(&stats->ls_lock);
1048 /* initialize the ls_percpu[cpuid] non-zero counter */
1049 for (i = 0; i < stats->ls_num; ++i) {
1050 cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1051 cntr->lc_min = LC_MIN_INIT;
1057 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1058 enum lprocfs_stats_flags flags)
1060 struct lprocfs_stats *stats;
1061 unsigned int num_entry;
1062 unsigned int percpusize = 0;
1068 if (lprocfs_no_percpu_stats != 0)
1069 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1071 if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1074 num_entry = num_possible_cpus();
1076 /* alloc percpu pointers for all possible cpu slots */
1077 LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1081 stats->ls_num = num;
1082 stats->ls_flags = flags;
1083 spin_lock_init(&stats->ls_lock);
1085 /* alloc num of counter headers */
1086 LIBCFS_ALLOC(stats->ls_cnt_header,
1087 stats->ls_num * sizeof(struct lprocfs_counter_header));
1088 if (stats->ls_cnt_header == NULL)
1091 if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1092 /* contains only one set counters */
1093 percpusize = lprocfs_stats_counter_size(stats);
1094 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1095 if (stats->ls_percpu[0] == NULL)
1097 stats->ls_biggest_alloc_num = 1;
1098 } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1099 /* alloc all percpu data, currently only obd_memory use this */
1100 for (i = 0; i < num_entry; ++i)
1101 if (lprocfs_stats_alloc_one(stats, i) < 0)
1108 lprocfs_free_stats(&stats);
1111 EXPORT_SYMBOL(lprocfs_alloc_stats);
1113 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1115 struct lprocfs_stats *stats = *statsh;
1116 unsigned int num_entry;
1117 unsigned int percpusize;
1120 if (stats == NULL || stats->ls_num == 0)
1124 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1127 num_entry = num_possible_cpus();
1129 percpusize = lprocfs_stats_counter_size(stats);
1130 for (i = 0; i < num_entry; i++)
1131 if (stats->ls_percpu[i] != NULL)
1132 LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1133 if (stats->ls_cnt_header != NULL)
1134 LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
1135 sizeof(struct lprocfs_counter_header));
1136 LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1138 EXPORT_SYMBOL(lprocfs_free_stats);
1140 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1142 struct lprocfs_counter *percpu_cntr;
1145 unsigned int num_entry;
1146 unsigned long flags = 0;
1148 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1150 for (i = 0; i < num_entry; i++) {
1151 if (stats->ls_percpu[i] == NULL)
1153 for (j = 0; j < stats->ls_num; j++) {
1154 percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1155 percpu_cntr->lc_count = 0;
1156 percpu_cntr->lc_min = LC_MIN_INIT;
1157 percpu_cntr->lc_max = 0;
1158 percpu_cntr->lc_sumsquare = 0;
1159 percpu_cntr->lc_sum = 0;
1160 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1161 percpu_cntr->lc_sum_irq = 0;
1165 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1167 EXPORT_SYMBOL(lprocfs_clear_stats);
1169 static ssize_t lprocfs_stats_seq_write(struct file *file,
1170 const char __user *buf,
1171 size_t len, loff_t *off)
1173 struct seq_file *seq = file->private_data;
1174 struct lprocfs_stats *stats = seq->private;
1176 lprocfs_clear_stats(stats);
1181 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1183 struct lprocfs_stats *stats = p->private;
1185 return (*pos < stats->ls_num) ? pos : NULL;
1188 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1192 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1196 return lprocfs_stats_seq_start(p, pos);
1199 /* seq file export of one lprocfs counter */
1200 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1202 struct lprocfs_stats *stats = p->private;
1203 struct lprocfs_counter_header *hdr;
1204 struct lprocfs_counter ctr;
1205 int idx = *(loff_t *)v;
1211 do_gettimeofday(&now);
1212 rc = seq_printf(p, "%-25s %lu.%lu secs.usecs\n",
1213 "snapshot_time", now.tv_sec, now.tv_usec);
1218 hdr = &stats->ls_cnt_header[idx];
1219 lprocfs_stats_collect(stats, idx, &ctr);
1221 if (ctr.lc_count == 0)
1224 rc = seq_printf(p, "%-25s "LPD64" samples [%s]", hdr->lc_name,
1225 ctr.lc_count, hdr->lc_units);
1229 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1230 rc = seq_printf(p, " "LPD64" "LPD64" "LPD64,
1231 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1234 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1235 rc = seq_printf(p, " "LPD64, ctr.lc_sumsquare);
1239 rc = seq_printf(p, "\n");
1241 return (rc < 0) ? rc : 0;
1244 static const struct seq_operations lprocfs_stats_seq_sops = {
1245 .start = lprocfs_stats_seq_start,
1246 .stop = lprocfs_stats_seq_stop,
1247 .next = lprocfs_stats_seq_next,
1248 .show = lprocfs_stats_seq_show,
1251 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1253 struct seq_file *seq;
1256 rc = LPROCFS_ENTRY_CHECK(inode);
1260 rc = seq_open(file, &lprocfs_stats_seq_sops);
1263 seq = file->private_data;
1264 seq->private = PDE_DATA(inode);
1268 static const struct file_operations lprocfs_stats_seq_fops = {
1269 .owner = THIS_MODULE,
1270 .open = lprocfs_stats_seq_open,
1272 .write = lprocfs_stats_seq_write,
1273 .llseek = seq_lseek,
1274 .release = lprocfs_seq_release,
1277 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1278 struct lprocfs_stats *stats)
1280 struct proc_dir_entry *entry;
1281 LASSERT(root != NULL);
1283 entry = proc_create_data(name, 0644, root,
1284 &lprocfs_stats_seq_fops, stats);
1289 EXPORT_SYMBOL(lprocfs_register_stats);
1291 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1292 unsigned conf, const char *name, const char *units)
1294 struct lprocfs_counter_header *header;
1295 struct lprocfs_counter *percpu_cntr;
1296 unsigned long flags = 0;
1298 unsigned int num_cpu;
1300 LASSERT(stats != NULL);
1302 header = &stats->ls_cnt_header[index];
1303 LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1304 index, name, units);
1306 header->lc_config = conf;
1307 header->lc_name = name;
1308 header->lc_units = units;
1310 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1311 for (i = 0; i < num_cpu; ++i) {
1312 if (stats->ls_percpu[i] == NULL)
1314 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1315 percpu_cntr->lc_count = 0;
1316 percpu_cntr->lc_min = LC_MIN_INIT;
1317 percpu_cntr->lc_max = 0;
1318 percpu_cntr->lc_sumsquare = 0;
1319 percpu_cntr->lc_sum = 0;
1320 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1321 percpu_cntr->lc_sum_irq = 0;
1323 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1325 EXPORT_SYMBOL(lprocfs_counter_init);
1327 /* Note that we only init md counters for ops whose offset is less
1328 * than NUM_MD_STATS. This is explained in a comment in the definition
1329 * of struct md_ops. */
1330 #define LPROCFS_MD_OP_INIT(base, stats, op) \
1332 unsigned int _idx = base + MD_COUNTER_OFFSET(op); \
1334 if (MD_COUNTER_OFFSET(op) < NUM_MD_STATS) { \
1335 LASSERT(_idx < stats->ls_num); \
1336 lprocfs_counter_init(stats, _idx, 0, #op, "reqs"); \
1340 void lprocfs_init_mps_stats(int num_private_stats, struct lprocfs_stats *stats)
1342 LPROCFS_MD_OP_INIT(num_private_stats, stats, getstatus);
1343 LPROCFS_MD_OP_INIT(num_private_stats, stats, null_inode);
1344 LPROCFS_MD_OP_INIT(num_private_stats, stats, find_cbdata);
1345 LPROCFS_MD_OP_INIT(num_private_stats, stats, close);
1346 LPROCFS_MD_OP_INIT(num_private_stats, stats, create);
1347 LPROCFS_MD_OP_INIT(num_private_stats, stats, done_writing);
1348 LPROCFS_MD_OP_INIT(num_private_stats, stats, enqueue);
1349 LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr);
1350 LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr_name);
1351 LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_lock);
1352 LPROCFS_MD_OP_INIT(num_private_stats, stats, link);
1353 LPROCFS_MD_OP_INIT(num_private_stats, stats, rename);
1354 LPROCFS_MD_OP_INIT(num_private_stats, stats, setattr);
1355 LPROCFS_MD_OP_INIT(num_private_stats, stats, fsync);
1356 LPROCFS_MD_OP_INIT(num_private_stats, stats, read_page);
1357 LPROCFS_MD_OP_INIT(num_private_stats, stats, unlink);
1358 LPROCFS_MD_OP_INIT(num_private_stats, stats, setxattr);
1359 LPROCFS_MD_OP_INIT(num_private_stats, stats, getxattr);
1360 LPROCFS_MD_OP_INIT(num_private_stats, stats, init_ea_size);
1361 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_lustre_md);
1362 LPROCFS_MD_OP_INIT(num_private_stats, stats, free_lustre_md);
1363 LPROCFS_MD_OP_INIT(num_private_stats, stats, merge_attr);
1364 LPROCFS_MD_OP_INIT(num_private_stats, stats, set_open_replay_data);
1365 LPROCFS_MD_OP_INIT(num_private_stats, stats, clear_open_replay_data);
1366 LPROCFS_MD_OP_INIT(num_private_stats, stats, set_lock_data);
1367 LPROCFS_MD_OP_INIT(num_private_stats, stats, lock_match);
1368 LPROCFS_MD_OP_INIT(num_private_stats, stats, cancel_unused);
1369 LPROCFS_MD_OP_INIT(num_private_stats, stats, renew_capa);
1370 LPROCFS_MD_OP_INIT(num_private_stats, stats, unpack_capa);
1371 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_remote_perm);
1372 LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_getattr_async);
1373 LPROCFS_MD_OP_INIT(num_private_stats, stats, revalidate_lock);
1376 int lprocfs_alloc_md_stats(struct obd_device *obd,
1377 unsigned int num_private_stats)
1379 struct lprocfs_stats *stats;
1380 unsigned int num_stats;
1383 CLASSERT(offsetof(struct md_ops, MD_STATS_FIRST_OP) == 0);
1384 CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_FIRST_OP) == 0);
1385 CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_LAST_OP) > 0);
1387 /* TODO Ensure that this function is only used where
1388 * appropriate by adding an assertion to the effect that
1389 * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1390 * because mdt_procfs_init() uses this function to allocate
1391 * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1392 * mdt layer does not use the md_ops interface. This is
1393 * confusing and a waste of memory. See LU-2484.
1395 LASSERT(obd->obd_proc_entry != NULL);
1396 LASSERT(obd->obd_md_stats == NULL);
1397 LASSERT(obd->obd_md_cntr_base == 0);
1399 num_stats = NUM_MD_STATS + num_private_stats;
1400 stats = lprocfs_alloc_stats(num_stats, 0);
1404 lprocfs_init_mps_stats(num_private_stats, stats);
1406 for (i = num_private_stats; i < num_stats; i++) {
1407 if (stats->ls_cnt_header[i].lc_name == NULL) {
1408 CERROR("Missing md_stat initializer md_op "
1409 "operation at offset %d. Aborting.\n",
1410 i - num_private_stats);
1415 rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1417 lprocfs_free_stats(&stats);
1419 obd->obd_md_stats = stats;
1420 obd->obd_md_cntr_base = num_private_stats;
1425 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1427 void lprocfs_free_md_stats(struct obd_device *obd)
1429 struct lprocfs_stats *stats = obd->obd_md_stats;
1431 if (stats != NULL) {
1432 obd->obd_md_stats = NULL;
1433 obd->obd_md_cntr_base = 0;
1434 lprocfs_free_stats(&stats);
1437 EXPORT_SYMBOL(lprocfs_free_md_stats);
1439 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1441 lprocfs_counter_init(ldlm_stats,
1442 LDLM_ENQUEUE - LDLM_FIRST_OPC,
1443 0, "ldlm_enqueue", "reqs");
1444 lprocfs_counter_init(ldlm_stats,
1445 LDLM_CONVERT - LDLM_FIRST_OPC,
1446 0, "ldlm_convert", "reqs");
1447 lprocfs_counter_init(ldlm_stats,
1448 LDLM_CANCEL - LDLM_FIRST_OPC,
1449 0, "ldlm_cancel", "reqs");
1450 lprocfs_counter_init(ldlm_stats,
1451 LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1452 0, "ldlm_bl_callback", "reqs");
1453 lprocfs_counter_init(ldlm_stats,
1454 LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1455 0, "ldlm_cp_callback", "reqs");
1456 lprocfs_counter_init(ldlm_stats,
1457 LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1458 0, "ldlm_gl_callback", "reqs");
1460 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1462 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1463 struct lprocfs_counter_header *header,
1464 enum lprocfs_stats_flags flags,
1465 enum lprocfs_fields_flags field)
1469 if (lc == NULL || header == NULL)
1473 case LPROCFS_FIELDS_FLAGS_CONFIG:
1474 ret = header->lc_config;
1476 case LPROCFS_FIELDS_FLAGS_SUM:
1478 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1479 ret += lc->lc_sum_irq;
1481 case LPROCFS_FIELDS_FLAGS_MIN:
1484 case LPROCFS_FIELDS_FLAGS_MAX:
1487 case LPROCFS_FIELDS_FLAGS_AVG:
1488 ret = (lc->lc_max - lc->lc_min) / 2;
1490 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1491 ret = lc->lc_sumsquare;
1493 case LPROCFS_FIELDS_FLAGS_COUNT:
1501 EXPORT_SYMBOL(lprocfs_read_helper);
1503 int lprocfs_write_helper(const char __user *buffer, unsigned long count,
1506 return lprocfs_write_frac_helper(buffer, count, val, 1);
1508 EXPORT_SYMBOL(lprocfs_write_helper);
1510 int lprocfs_write_frac_helper(const char __user *buffer, unsigned long count,
1513 char kernbuf[20], *end, *pbuf;
1515 if (count > (sizeof(kernbuf) - 1))
1518 if (copy_from_user(kernbuf, buffer, count))
1521 kernbuf[count] = '\0';
1528 *val = (int)simple_strtoul(pbuf, &end, 10) * mult;
1532 if (end != NULL && *end == '.') {
1533 int temp_val, pow = 1;
1537 if (strlen(pbuf) > 5)
1538 pbuf[5] = '\0'; /*only allow 5bits fractional*/
1540 temp_val = (int)simple_strtoul(pbuf, &end, 10) * mult;
1543 for (i = 0; i < (end - pbuf); i++)
1546 *val += temp_val / pow;
1551 EXPORT_SYMBOL(lprocfs_write_frac_helper);
1553 int lprocfs_read_frac_helper(char *buffer, unsigned long count, long val,
1556 long decimal_val, frac_val;
1562 decimal_val = val / mult;
1563 prtn = snprintf(buffer, count, "%ld", decimal_val);
1564 frac_val = val % mult;
1566 if (prtn < (count - 4) && frac_val > 0) {
1568 int i, temp_mult = 1, frac_bits = 0;
1570 temp_frac = frac_val * 10;
1571 buffer[prtn++] = '.';
1572 while (frac_bits < 2 && (temp_frac / mult) < 1 ) {
1573 /* only reserved 2 bits fraction */
1574 buffer[prtn++] ='0';
1579 * Need to think these cases :
1580 * 1. #echo x.00 > /proc/xxx output result : x
1581 * 2. #echo x.0x > /proc/xxx output result : x.0x
1582 * 3. #echo x.x0 > /proc/xxx output result : x.x
1583 * 4. #echo x.xx > /proc/xxx output result : x.xx
1584 * Only reserved 2 bits fraction.
1586 for (i = 0; i < (5 - prtn); i++)
1589 frac_bits = min((int)count - prtn, 3 - frac_bits);
1590 prtn += snprintf(buffer + prtn, frac_bits, "%ld",
1591 frac_val * temp_mult / mult);
1594 while(buffer[prtn] < '1' || buffer[prtn] > '9') {
1596 if (buffer[prtn] == '.') {
1603 buffer[prtn++] ='\n';
1607 int lprocfs_seq_read_frac_helper(struct seq_file *m, long val, int mult)
1609 long decimal_val, frac_val;
1611 decimal_val = val / mult;
1612 seq_printf(m, "%ld", decimal_val);
1613 frac_val = val % mult;
1620 /* Three cases: x0, xx, 0x */
1621 if ((frac_val % 10) != 0)
1622 seq_printf(m, ".%ld", frac_val);
1624 seq_printf(m, ".%ld", frac_val / 10);
1627 seq_printf(m, "\n");
1630 EXPORT_SYMBOL(lprocfs_seq_read_frac_helper);
1632 int lprocfs_write_u64_helper(const char __user *buffer, unsigned long count,
1635 return lprocfs_write_frac_u64_helper(buffer, count, val, 1);
1637 EXPORT_SYMBOL(lprocfs_write_u64_helper);
1639 int lprocfs_write_frac_u64_helper(const char __user *buffer,
1640 unsigned long count,
1641 __u64 *val, int mult)
1643 char kernbuf[22], *end, *pbuf;
1644 __u64 whole, frac = 0, units;
1645 unsigned frac_d = 1;
1647 if (count > (sizeof(kernbuf) - 1))
1650 if (copy_from_user(kernbuf, buffer, count))
1653 kernbuf[count] = '\0';
1660 whole = simple_strtoull(pbuf, &end, 10);
1664 if (end != NULL && *end == '.') {
1668 /* need to limit frac_d to a __u32 */
1669 if (strlen(pbuf) > 10)
1672 frac = simple_strtoull(pbuf, &end, 10);
1673 /* count decimal places */
1674 for (i = 0; i < (end - pbuf); i++)
1693 /* Specified units override the multiplier */
1695 mult = mult < 0 ? -units : units;
1698 do_div(frac, frac_d);
1699 *val = whole * mult + frac;
1702 EXPORT_SYMBOL(lprocfs_write_frac_u64_helper);
1704 static char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1713 if (!memcmp(s1, s2, l2))
1721 * Find the string \a name in the input \a buffer, and return a pointer to the
1722 * value immediately following \a name, reducing \a count appropriately.
1723 * If \a name is not found the original \a buffer is returned.
1725 char *lprocfs_find_named_value(const char *buffer, const char *name,
1729 size_t buflen = *count;
1731 /* there is no strnstr() in rhel5 and ubuntu kernels */
1732 val = lprocfs_strnstr(buffer, name, buflen);
1734 return (char *)buffer;
1736 val += strlen(name); /* skip prefix */
1737 while (val < buffer + buflen && isspace(*val)) /* skip separator */
1741 while (val < buffer + buflen && isalnum(*val)) {
1746 return val - *count;
1748 EXPORT_SYMBOL(lprocfs_find_named_value);
1750 int lprocfs_seq_create(struct proc_dir_entry *parent,
1753 const struct file_operations *seq_fops,
1756 struct proc_dir_entry *entry;
1759 /* Disallow secretly (un)writable entries. */
1760 LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
1762 entry = proc_create_data(name, mode, parent, seq_fops, data);
1769 EXPORT_SYMBOL(lprocfs_seq_create);
1771 int lprocfs_obd_seq_create(struct obd_device *dev,
1774 const struct file_operations *seq_fops,
1777 return (lprocfs_seq_create(dev->obd_proc_entry, name,
1778 mode, seq_fops, data));
1780 EXPORT_SYMBOL(lprocfs_obd_seq_create);
1782 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
1784 if (value >= OBD_HIST_MAX)
1785 value = OBD_HIST_MAX - 1;
1787 spin_lock(&oh->oh_lock);
1788 oh->oh_buckets[value]++;
1789 spin_unlock(&oh->oh_lock);
1791 EXPORT_SYMBOL(lprocfs_oh_tally);
1793 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
1795 unsigned int val = 0;
1797 if (likely(value != 0))
1798 val = min(fls(value - 1), OBD_HIST_MAX);
1800 lprocfs_oh_tally(oh, val);
1802 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
1804 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
1806 unsigned long ret = 0;
1809 for (i = 0; i < OBD_HIST_MAX; i++)
1810 ret += oh->oh_buckets[i];
1813 EXPORT_SYMBOL(lprocfs_oh_sum);
1815 void lprocfs_oh_clear(struct obd_histogram *oh)
1817 spin_lock(&oh->oh_lock);
1818 memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
1819 spin_unlock(&oh->oh_lock);
1821 EXPORT_SYMBOL(lprocfs_oh_clear);
1823 int lprocfs_obd_rd_max_pages_per_rpc(char *page, char **start, off_t off,
1824 int count, int *eof, void *data)
1826 struct obd_device *dev = data;
1827 struct client_obd *cli = &dev->u.cli;
1830 spin_lock(&cli->cl_loi_list_lock);
1831 rc = snprintf(page, count, "%d\n", cli->cl_max_pages_per_rpc);
1832 spin_unlock(&cli->cl_loi_list_lock);
1837 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
1839 struct obd_device *dev = data;
1840 struct client_obd *cli = &dev->u.cli;
1843 spin_lock(&cli->cl_loi_list_lock);
1844 rc = seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
1845 spin_unlock(&cli->cl_loi_list_lock);
1848 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
1850 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
1851 struct root_squash_info *squash, char *name)
1854 char kernbuf[64], *tmp, *errmsg;
1855 unsigned long uid, gid;
1858 if (count >= sizeof(kernbuf)) {
1859 errmsg = "string too long";
1860 GOTO(failed_noprint, rc = -EINVAL);
1862 if (copy_from_user(kernbuf, buffer, count)) {
1863 errmsg = "bad address";
1864 GOTO(failed_noprint, rc = -EFAULT);
1866 kernbuf[count] = '\0';
1868 /* look for uid gid separator */
1869 tmp = strchr(kernbuf, ':');
1871 errmsg = "needs uid:gid format";
1872 GOTO(failed, rc = -EINVAL);
1878 if (kstrtoul(kernbuf, 0, &uid) != 0) {
1880 GOTO(failed, rc = -EINVAL);
1884 if (kstrtoul(tmp, 0, &gid) != 0) {
1886 GOTO(failed, rc = -EINVAL);
1889 squash->rsi_uid = uid;
1890 squash->rsi_gid = gid;
1892 LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
1893 name, squash->rsi_uid, squash->rsi_gid);
1901 CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
1902 name, kernbuf, errmsg, rc);
1905 CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
1909 EXPORT_SYMBOL(lprocfs_wr_root_squash);
1912 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
1913 struct root_squash_info *squash, char *name)
1916 char *kernbuf = NULL;
1918 struct list_head tmp;
1922 errmsg = "string too long";
1923 GOTO(failed, rc = -EINVAL);
1926 OBD_ALLOC(kernbuf, count + 1);
1927 if (kernbuf == NULL) {
1928 errmsg = "no memory";
1929 GOTO(failed, rc = -ENOMEM);
1931 if (copy_from_user(kernbuf, buffer, count)) {
1932 errmsg = "bad address";
1933 GOTO(failed, rc = -EFAULT);
1935 kernbuf[count] = '\0';
1937 if (count > 0 && kernbuf[count - 1] == '\n')
1938 kernbuf[count - 1] = '\0';
1940 if (strcmp(kernbuf, "NONE") == 0 || strcmp(kernbuf, "clear") == 0) {
1941 /* empty string is special case */
1942 down_write(&squash->rsi_sem);
1943 if (!list_empty(&squash->rsi_nosquash_nids))
1944 cfs_free_nidlist(&squash->rsi_nosquash_nids);
1945 up_write(&squash->rsi_sem);
1946 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
1947 OBD_FREE(kernbuf, count + 1);
1951 INIT_LIST_HEAD(&tmp);
1952 if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
1953 errmsg = "can't parse";
1954 GOTO(failed, rc = -EINVAL);
1956 LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
1958 OBD_FREE(kernbuf, count + 1);
1961 down_write(&squash->rsi_sem);
1962 if (!list_empty(&squash->rsi_nosquash_nids))
1963 cfs_free_nidlist(&squash->rsi_nosquash_nids);
1964 list_splice(&tmp, &squash->rsi_nosquash_nids);
1965 up_write(&squash->rsi_sem);
1971 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
1972 name, kernbuf, errmsg, rc);
1973 OBD_FREE(kernbuf, count + 1);
1975 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
1980 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
1982 #endif /* CONFIG_PROC_FS*/