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 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",
663 static void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, char *sep)
669 for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
671 seq_printf(m, "%s%s",
672 first ? "" : sep, obd_connect_names[i]);
676 if (flags & ~(mask - 1))
677 seq_printf(m, "%sunknown_"LPX64,
678 first ? "" : sep, flags & ~(mask - 1));
681 int obd_connect_flags2str(char *page, int count, __u64 flags, char *sep)
686 for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
688 ret += snprintf(page + ret, count - ret, "%s%s",
689 ret ? sep : "", obd_connect_names[i]);
691 if (flags & ~(mask - 1))
692 ret += snprintf(page + ret, count - ret,
694 ret ? sep : "", flags & ~(mask - 1));
697 EXPORT_SYMBOL(obd_connect_flags2str);
699 static void obd_connect_data_seqprint(struct seq_file *m,
700 struct obd_connect_data *ocd)
704 LASSERT(ocd != NULL);
705 flags = ocd->ocd_connect_flags;
707 seq_printf(m, " connect_data:\n"
710 ocd->ocd_connect_flags,
712 if (flags & OBD_CONNECT_VERSION)
713 seq_printf(m, " target_version: %u.%u.%u.%u\n",
714 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
715 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
716 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
717 OBD_OCD_VERSION_FIX(ocd->ocd_version));
718 if (flags & OBD_CONNECT_MDS)
719 seq_printf(m, " mdt_index: %d\n", ocd->ocd_group);
720 if (flags & OBD_CONNECT_GRANT)
721 seq_printf(m, " initial_grant: %d\n", ocd->ocd_grant);
722 if (flags & OBD_CONNECT_INDEX)
723 seq_printf(m, " target_index: %u\n", ocd->ocd_index);
724 if (flags & OBD_CONNECT_BRW_SIZE)
725 seq_printf(m, " max_brw_size: %d\n", ocd->ocd_brw_size);
726 if (flags & OBD_CONNECT_IBITS)
727 seq_printf(m, " ibits_known: "LPX64"\n",
728 ocd->ocd_ibits_known);
729 if (flags & OBD_CONNECT_GRANT_PARAM)
730 seq_printf(m, " grant_block_size: %d\n"
731 " grant_inode_size: %d\n"
732 " grant_extent_overhead: %d\n",
735 ocd->ocd_grant_extent);
736 if (flags & OBD_CONNECT_TRANSNO)
737 seq_printf(m, " first_transno: "LPX64"\n",
739 if (flags & OBD_CONNECT_CKSUM)
740 seq_printf(m, " cksum_types: %#x\n",
741 ocd->ocd_cksum_types);
742 if (flags & OBD_CONNECT_MAX_EASIZE)
743 seq_printf(m, " max_easize: %d\n", ocd->ocd_max_easize);
744 if (flags & OBD_CONNECT_MAXBYTES)
745 seq_printf(m, " max_object_bytes: "LPU64"\n",
747 if (flags & OBD_CONNECT_MULTIMODRPCS)
748 seq_printf(m, " max_mod_rpcs: %hu\n",
749 ocd->ocd_maxmodrpcs);
752 int lprocfs_import_seq_show(struct seq_file *m, void *data)
754 char nidstr[LNET_NIDSTR_SIZE];
755 struct lprocfs_counter ret;
756 struct lprocfs_counter_header *header;
757 struct obd_device *obd = (struct obd_device *)data;
758 struct obd_import *imp;
759 struct obd_import_conn *conn;
760 struct obd_connect_data *ocd;
765 LASSERT(obd != NULL);
766 LPROCFS_CLIMP_CHECK(obd);
767 imp = obd->u.cli.cl_import;
768 ocd = &imp->imp_connect_data;
770 seq_printf(m, "import:\n"
774 " connect_flags: [ ",
777 ptlrpc_import_state_name(imp->imp_state));
778 obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
780 seq_printf(m, " ]\n");
781 obd_connect_data_seqprint(m, ocd);
782 seq_printf(m, " import_flags: [ ");
783 obd_import_flags2str(imp, m);
787 " failover_nids: [ ");
788 spin_lock(&imp->imp_lock);
790 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
791 libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
792 nidstr, sizeof(nidstr));
793 seq_printf(m, "%s%s", j ? ", " : "", nidstr);
796 if (imp->imp_connection != NULL)
797 libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
798 nidstr, sizeof(nidstr));
800 strncpy(nidstr, "<none>", sizeof(nidstr));
802 " current_connection: %s\n"
803 " connection_attempts: %u\n"
805 " in-progress_invalidations: %u\n",
809 atomic_read(&imp->imp_inval_count));
810 spin_unlock(&imp->imp_lock);
812 if (obd->obd_svc_stats == NULL)
815 header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
816 lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
817 if (ret.lc_count != 0) {
818 /* first argument to do_div MUST be __u64 */
819 __u64 sum = ret.lc_sum;
820 do_div(sum, ret.lc_count);
824 seq_printf(m, " rpcs:\n"
826 " unregistering: %u\n"
828 " avg_waittime: "LPU64" %s\n",
829 atomic_read(&imp->imp_inflight),
830 atomic_read(&imp->imp_unregistering),
831 atomic_read(&imp->imp_timeouts),
832 ret.lc_sum, header->lc_units);
835 for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
836 if (imp->imp_at.iat_portal[j] == 0)
838 k = max_t(unsigned int, k,
839 at_get(&imp->imp_at.iat_service_estimate[j]));
841 seq_printf(m, " service_estimates:\n"
842 " services: %u sec\n"
843 " network: %u sec\n",
845 at_get(&imp->imp_at.iat_net_latency));
847 seq_printf(m, " transactions:\n"
848 " last_replay: "LPU64"\n"
849 " peer_committed: "LPU64"\n"
850 " last_checked: "LPU64"\n",
851 imp->imp_last_replay_transno,
852 imp->imp_peer_committed_transno,
853 imp->imp_last_transno_checked);
856 for (rw = 0; rw <= 1; rw++) {
857 lprocfs_stats_collect(obd->obd_svc_stats,
858 PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
860 if (ret.lc_sum > 0 && ret.lc_count > 0) {
861 /* first argument to do_div MUST be __u64 */
862 __u64 sum = ret.lc_sum;
863 do_div(sum, ret.lc_count);
865 seq_printf(m, " %s_data_averages:\n"
866 " bytes_per_rpc: "LPU64"\n",
867 rw ? "write" : "read",
871 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
872 header = &obd->obd_svc_stats->ls_cnt_header[j];
873 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
874 if (ret.lc_sum > 0 && ret.lc_count != 0) {
875 /* first argument to do_div MUST be __u64 */
876 __u64 sum = ret.lc_sum;
877 do_div(sum, ret.lc_count);
879 seq_printf(m, " %s_per_rpc: "LPU64"\n",
880 header->lc_units, ret.lc_sum);
883 seq_printf(m, " MB_per_sec: %u.%.02u\n",
884 k / j, (100 * k / j) % 100);
889 LPROCFS_CLIMP_EXIT(obd);
892 EXPORT_SYMBOL(lprocfs_import_seq_show);
894 int lprocfs_state_seq_show(struct seq_file *m, void *data)
896 struct obd_device *obd = (struct obd_device *)data;
897 struct obd_import *imp;
900 LASSERT(obd != NULL);
901 LPROCFS_CLIMP_CHECK(obd);
902 imp = obd->u.cli.cl_import;
904 seq_printf(m, "current_state: %s\n",
905 ptlrpc_import_state_name(imp->imp_state));
906 seq_printf(m, "state_history:\n");
907 k = imp->imp_state_hist_idx;
908 for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
909 struct import_state_hist *ish =
910 &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
911 if (ish->ish_state == 0)
913 seq_printf(m, " - [ "CFS_TIME_T", %s ]\n",
915 ptlrpc_import_state_name(ish->ish_state));
918 LPROCFS_CLIMP_EXIT(obd);
921 EXPORT_SYMBOL(lprocfs_state_seq_show);
923 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
926 for (i = 0; i < AT_BINS; i++)
927 seq_printf(m, "%3u ", at->at_hist[i]);
931 EXPORT_SYMBOL(lprocfs_at_hist_helper);
933 /* See also ptlrpc_lprocfs_timeouts_show_seq */
934 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
936 struct obd_device *obd = (struct obd_device *)data;
937 struct obd_import *imp;
938 unsigned int cur, worst;
943 LASSERT(obd != NULL);
944 LPROCFS_CLIMP_CHECK(obd);
945 imp = obd->u.cli.cl_import;
947 now = cfs_time_current_sec();
949 /* Some network health info for kicks */
950 s2dhms(&ts, now - imp->imp_last_reply_time);
951 seq_printf(m, "%-10s : %ld, "DHMS_FMT" ago\n",
952 "last reply", imp->imp_last_reply_time, DHMS_VARS(&ts));
954 cur = at_get(&imp->imp_at.iat_net_latency);
955 worst = imp->imp_at.iat_net_latency.at_worst_ever;
956 worstt = imp->imp_at.iat_net_latency.at_worst_time;
957 s2dhms(&ts, now - worstt);
958 seq_printf(m, "%-10s : cur %3u worst %3u (at %ld, "DHMS_FMT" ago) ",
959 "network", cur, worst, worstt, DHMS_VARS(&ts));
960 lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
962 for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
963 if (imp->imp_at.iat_portal[i] == 0)
965 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
966 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
967 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
968 s2dhms(&ts, now - worstt);
969 seq_printf(m, "portal %-2d : cur %3u worst %3u (at %ld, "
970 DHMS_FMT" ago) ", imp->imp_at.iat_portal[i],
971 cur, worst, worstt, DHMS_VARS(&ts));
972 lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
975 LPROCFS_CLIMP_EXIT(obd);
978 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
980 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
982 struct obd_device *obd = data;
985 LPROCFS_CLIMP_CHECK(obd);
986 flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
987 seq_printf(m, "flags="LPX64"\n", flags);
988 obd_connect_seq_flags2str(m, flags, "\n");
990 LPROCFS_CLIMP_EXIT(obd);
993 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
996 lprocfs_obd_setup(struct obd_device *obd)
1000 LASSERT(obd != NULL);
1001 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1002 LASSERT(obd->obd_type->typ_procroot != NULL);
1004 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1005 obd->obd_type->typ_procroot,
1006 obd->obd_vars, obd);
1007 if (IS_ERR(obd->obd_proc_entry)) {
1008 rc = PTR_ERR(obd->obd_proc_entry);
1009 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1010 obd->obd_proc_entry = NULL;
1014 EXPORT_SYMBOL(lprocfs_obd_setup);
1016 int lprocfs_obd_cleanup(struct obd_device *obd)
1020 if (obd->obd_proc_exports_entry) {
1021 /* Should be no exports left */
1022 lprocfs_remove(&obd->obd_proc_exports_entry);
1023 obd->obd_proc_exports_entry = NULL;
1025 if (obd->obd_proc_entry) {
1026 lprocfs_remove(&obd->obd_proc_entry);
1027 obd->obd_proc_entry = NULL;
1031 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1033 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1035 struct lprocfs_counter *cntr;
1036 unsigned int percpusize;
1038 unsigned long flags = 0;
1041 LASSERT(stats->ls_percpu[cpuid] == NULL);
1042 LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1044 percpusize = lprocfs_stats_counter_size(stats);
1045 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1046 if (stats->ls_percpu[cpuid] != NULL) {
1048 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1049 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1050 spin_lock_irqsave(&stats->ls_lock, flags);
1052 spin_lock(&stats->ls_lock);
1053 if (stats->ls_biggest_alloc_num <= cpuid)
1054 stats->ls_biggest_alloc_num = cpuid + 1;
1055 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1056 spin_unlock_irqrestore(&stats->ls_lock, flags);
1058 spin_unlock(&stats->ls_lock);
1061 /* initialize the ls_percpu[cpuid] non-zero counter */
1062 for (i = 0; i < stats->ls_num; ++i) {
1063 cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1064 cntr->lc_min = LC_MIN_INIT;
1069 EXPORT_SYMBOL(lprocfs_stats_alloc_one);
1071 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1072 enum lprocfs_stats_flags flags)
1074 struct lprocfs_stats *stats;
1075 unsigned int num_entry;
1076 unsigned int percpusize = 0;
1082 if (lprocfs_no_percpu_stats != 0)
1083 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1085 if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1088 num_entry = num_possible_cpus();
1090 /* alloc percpu pointers for all possible cpu slots */
1091 LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1095 stats->ls_num = num;
1096 stats->ls_flags = flags;
1097 spin_lock_init(&stats->ls_lock);
1099 /* alloc num of counter headers */
1100 LIBCFS_ALLOC(stats->ls_cnt_header,
1101 stats->ls_num * sizeof(struct lprocfs_counter_header));
1102 if (stats->ls_cnt_header == NULL)
1105 if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1106 /* contains only one set counters */
1107 percpusize = lprocfs_stats_counter_size(stats);
1108 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1109 if (stats->ls_percpu[0] == NULL)
1111 stats->ls_biggest_alloc_num = 1;
1112 } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1113 /* alloc all percpu data, currently only obd_memory use this */
1114 for (i = 0; i < num_entry; ++i)
1115 if (lprocfs_stats_alloc_one(stats, i) < 0)
1122 lprocfs_free_stats(&stats);
1125 EXPORT_SYMBOL(lprocfs_alloc_stats);
1127 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1129 struct lprocfs_stats *stats = *statsh;
1130 unsigned int num_entry;
1131 unsigned int percpusize;
1134 if (stats == NULL || stats->ls_num == 0)
1138 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1141 num_entry = num_possible_cpus();
1143 percpusize = lprocfs_stats_counter_size(stats);
1144 for (i = 0; i < num_entry; i++)
1145 if (stats->ls_percpu[i] != NULL)
1146 LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1147 if (stats->ls_cnt_header != NULL)
1148 LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
1149 sizeof(struct lprocfs_counter_header));
1150 LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1152 EXPORT_SYMBOL(lprocfs_free_stats);
1154 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1156 struct lprocfs_counter *percpu_cntr;
1159 unsigned int num_entry;
1160 unsigned long flags = 0;
1162 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1164 for (i = 0; i < num_entry; i++) {
1165 if (stats->ls_percpu[i] == NULL)
1167 for (j = 0; j < stats->ls_num; j++) {
1168 percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1169 percpu_cntr->lc_count = 0;
1170 percpu_cntr->lc_min = LC_MIN_INIT;
1171 percpu_cntr->lc_max = 0;
1172 percpu_cntr->lc_sumsquare = 0;
1173 percpu_cntr->lc_sum = 0;
1174 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1175 percpu_cntr->lc_sum_irq = 0;
1179 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1181 EXPORT_SYMBOL(lprocfs_clear_stats);
1183 static ssize_t lprocfs_stats_seq_write(struct file *file,
1184 const char __user *buf,
1185 size_t len, loff_t *off)
1187 struct seq_file *seq = file->private_data;
1188 struct lprocfs_stats *stats = seq->private;
1190 lprocfs_clear_stats(stats);
1195 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1197 struct lprocfs_stats *stats = p->private;
1199 return (*pos < stats->ls_num) ? pos : NULL;
1202 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1206 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1210 return lprocfs_stats_seq_start(p, pos);
1213 /* seq file export of one lprocfs counter */
1214 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1216 struct lprocfs_stats *stats = p->private;
1217 struct lprocfs_counter_header *hdr;
1218 struct lprocfs_counter ctr;
1219 int idx = *(loff_t *)v;
1225 do_gettimeofday(&now);
1226 rc = seq_printf(p, "%-25s %lu.%lu secs.usecs\n",
1227 "snapshot_time", now.tv_sec, now.tv_usec);
1232 hdr = &stats->ls_cnt_header[idx];
1233 lprocfs_stats_collect(stats, idx, &ctr);
1235 if (ctr.lc_count == 0)
1238 rc = seq_printf(p, "%-25s "LPD64" samples [%s]", hdr->lc_name,
1239 ctr.lc_count, hdr->lc_units);
1243 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1244 rc = seq_printf(p, " "LPD64" "LPD64" "LPD64,
1245 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1248 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1249 rc = seq_printf(p, " "LPD64, ctr.lc_sumsquare);
1253 rc = seq_printf(p, "\n");
1255 return (rc < 0) ? rc : 0;
1258 static const struct seq_operations lprocfs_stats_seq_sops = {
1259 .start = lprocfs_stats_seq_start,
1260 .stop = lprocfs_stats_seq_stop,
1261 .next = lprocfs_stats_seq_next,
1262 .show = lprocfs_stats_seq_show,
1265 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1267 struct seq_file *seq;
1270 rc = LPROCFS_ENTRY_CHECK(inode);
1274 rc = seq_open(file, &lprocfs_stats_seq_sops);
1277 seq = file->private_data;
1278 seq->private = PDE_DATA(inode);
1282 static const struct file_operations lprocfs_stats_seq_fops = {
1283 .owner = THIS_MODULE,
1284 .open = lprocfs_stats_seq_open,
1286 .write = lprocfs_stats_seq_write,
1287 .llseek = seq_lseek,
1288 .release = lprocfs_seq_release,
1291 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1292 struct lprocfs_stats *stats)
1294 struct proc_dir_entry *entry;
1295 LASSERT(root != NULL);
1297 entry = proc_create_data(name, 0644, root,
1298 &lprocfs_stats_seq_fops, stats);
1303 EXPORT_SYMBOL(lprocfs_register_stats);
1305 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1306 unsigned conf, const char *name, const char *units)
1308 struct lprocfs_counter_header *header;
1309 struct lprocfs_counter *percpu_cntr;
1310 unsigned long flags = 0;
1312 unsigned int num_cpu;
1314 LASSERT(stats != NULL);
1316 header = &stats->ls_cnt_header[index];
1317 LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1318 index, name, units);
1320 header->lc_config = conf;
1321 header->lc_name = name;
1322 header->lc_units = units;
1324 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1325 for (i = 0; i < num_cpu; ++i) {
1326 if (stats->ls_percpu[i] == NULL)
1328 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1329 percpu_cntr->lc_count = 0;
1330 percpu_cntr->lc_min = LC_MIN_INIT;
1331 percpu_cntr->lc_max = 0;
1332 percpu_cntr->lc_sumsquare = 0;
1333 percpu_cntr->lc_sum = 0;
1334 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1335 percpu_cntr->lc_sum_irq = 0;
1337 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1339 EXPORT_SYMBOL(lprocfs_counter_init);
1341 /* Note that we only init md counters for ops whose offset is less
1342 * than NUM_MD_STATS. This is explained in a comment in the definition
1343 * of struct md_ops. */
1344 #define LPROCFS_MD_OP_INIT(base, stats, op) \
1346 unsigned int _idx = base + MD_COUNTER_OFFSET(op); \
1348 if (MD_COUNTER_OFFSET(op) < NUM_MD_STATS) { \
1349 LASSERT(_idx < stats->ls_num); \
1350 lprocfs_counter_init(stats, _idx, 0, #op, "reqs"); \
1354 void lprocfs_init_mps_stats(int num_private_stats, struct lprocfs_stats *stats)
1356 LPROCFS_MD_OP_INIT(num_private_stats, stats, getstatus);
1357 LPROCFS_MD_OP_INIT(num_private_stats, stats, null_inode);
1358 LPROCFS_MD_OP_INIT(num_private_stats, stats, find_cbdata);
1359 LPROCFS_MD_OP_INIT(num_private_stats, stats, close);
1360 LPROCFS_MD_OP_INIT(num_private_stats, stats, create);
1361 LPROCFS_MD_OP_INIT(num_private_stats, stats, enqueue);
1362 LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr);
1363 LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr_name);
1364 LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_lock);
1365 LPROCFS_MD_OP_INIT(num_private_stats, stats, link);
1366 LPROCFS_MD_OP_INIT(num_private_stats, stats, rename);
1367 LPROCFS_MD_OP_INIT(num_private_stats, stats, setattr);
1368 LPROCFS_MD_OP_INIT(num_private_stats, stats, fsync);
1369 LPROCFS_MD_OP_INIT(num_private_stats, stats, read_page);
1370 LPROCFS_MD_OP_INIT(num_private_stats, stats, unlink);
1371 LPROCFS_MD_OP_INIT(num_private_stats, stats, setxattr);
1372 LPROCFS_MD_OP_INIT(num_private_stats, stats, getxattr);
1373 LPROCFS_MD_OP_INIT(num_private_stats, stats, init_ea_size);
1374 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_lustre_md);
1375 LPROCFS_MD_OP_INIT(num_private_stats, stats, free_lustre_md);
1376 LPROCFS_MD_OP_INIT(num_private_stats, stats, merge_attr);
1377 LPROCFS_MD_OP_INIT(num_private_stats, stats, set_open_replay_data);
1378 LPROCFS_MD_OP_INIT(num_private_stats, stats, clear_open_replay_data);
1379 LPROCFS_MD_OP_INIT(num_private_stats, stats, set_lock_data);
1380 LPROCFS_MD_OP_INIT(num_private_stats, stats, lock_match);
1381 LPROCFS_MD_OP_INIT(num_private_stats, stats, cancel_unused);
1382 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_remote_perm);
1383 LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_getattr_async);
1384 LPROCFS_MD_OP_INIT(num_private_stats, stats, revalidate_lock);
1387 int lprocfs_alloc_md_stats(struct obd_device *obd,
1388 unsigned int num_private_stats)
1390 struct lprocfs_stats *stats;
1391 unsigned int num_stats;
1394 CLASSERT(offsetof(struct md_ops, MD_STATS_FIRST_OP) == 0);
1395 CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_FIRST_OP) == 0);
1396 CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_LAST_OP) > 0);
1398 /* TODO Ensure that this function is only used where
1399 * appropriate by adding an assertion to the effect that
1400 * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1401 * because mdt_procfs_init() uses this function to allocate
1402 * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1403 * mdt layer does not use the md_ops interface. This is
1404 * confusing and a waste of memory. See LU-2484.
1406 LASSERT(obd->obd_proc_entry != NULL);
1407 LASSERT(obd->obd_md_stats == NULL);
1408 LASSERT(obd->obd_md_cntr_base == 0);
1410 num_stats = NUM_MD_STATS + num_private_stats;
1411 stats = lprocfs_alloc_stats(num_stats, 0);
1415 lprocfs_init_mps_stats(num_private_stats, stats);
1417 for (i = num_private_stats; i < num_stats; i++) {
1418 if (stats->ls_cnt_header[i].lc_name == NULL) {
1419 CERROR("Missing md_stat initializer md_op "
1420 "operation at offset %d. Aborting.\n",
1421 i - num_private_stats);
1426 rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1428 lprocfs_free_stats(&stats);
1430 obd->obd_md_stats = stats;
1431 obd->obd_md_cntr_base = num_private_stats;
1436 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1438 void lprocfs_free_md_stats(struct obd_device *obd)
1440 struct lprocfs_stats *stats = obd->obd_md_stats;
1442 if (stats != NULL) {
1443 obd->obd_md_stats = NULL;
1444 obd->obd_md_cntr_base = 0;
1445 lprocfs_free_stats(&stats);
1448 EXPORT_SYMBOL(lprocfs_free_md_stats);
1450 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1452 lprocfs_counter_init(ldlm_stats,
1453 LDLM_ENQUEUE - LDLM_FIRST_OPC,
1454 0, "ldlm_enqueue", "reqs");
1455 lprocfs_counter_init(ldlm_stats,
1456 LDLM_CONVERT - LDLM_FIRST_OPC,
1457 0, "ldlm_convert", "reqs");
1458 lprocfs_counter_init(ldlm_stats,
1459 LDLM_CANCEL - LDLM_FIRST_OPC,
1460 0, "ldlm_cancel", "reqs");
1461 lprocfs_counter_init(ldlm_stats,
1462 LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1463 0, "ldlm_bl_callback", "reqs");
1464 lprocfs_counter_init(ldlm_stats,
1465 LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1466 0, "ldlm_cp_callback", "reqs");
1467 lprocfs_counter_init(ldlm_stats,
1468 LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1469 0, "ldlm_gl_callback", "reqs");
1471 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1473 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1474 struct lprocfs_counter_header *header,
1475 enum lprocfs_stats_flags flags,
1476 enum lprocfs_fields_flags field)
1480 if (lc == NULL || header == NULL)
1484 case LPROCFS_FIELDS_FLAGS_CONFIG:
1485 ret = header->lc_config;
1487 case LPROCFS_FIELDS_FLAGS_SUM:
1489 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1490 ret += lc->lc_sum_irq;
1492 case LPROCFS_FIELDS_FLAGS_MIN:
1495 case LPROCFS_FIELDS_FLAGS_MAX:
1498 case LPROCFS_FIELDS_FLAGS_AVG:
1499 ret = (lc->lc_max - lc->lc_min) / 2;
1501 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1502 ret = lc->lc_sumsquare;
1504 case LPROCFS_FIELDS_FLAGS_COUNT:
1512 EXPORT_SYMBOL(lprocfs_read_helper);
1514 int lprocfs_write_helper(const char __user *buffer, unsigned long count,
1517 return lprocfs_write_frac_helper(buffer, count, val, 1);
1519 EXPORT_SYMBOL(lprocfs_write_helper);
1521 int lprocfs_write_frac_helper(const char __user *buffer, unsigned long count,
1524 char kernbuf[20], *end, *pbuf;
1526 if (count > (sizeof(kernbuf) - 1))
1529 if (copy_from_user(kernbuf, buffer, count))
1532 kernbuf[count] = '\0';
1539 *val = (int)simple_strtoul(pbuf, &end, 10) * mult;
1543 if (end != NULL && *end == '.') {
1544 int temp_val, pow = 1;
1548 if (strlen(pbuf) > 5)
1549 pbuf[5] = '\0'; /*only allow 5bits fractional*/
1551 temp_val = (int)simple_strtoul(pbuf, &end, 10) * mult;
1554 for (i = 0; i < (end - pbuf); i++)
1557 *val += temp_val / pow;
1562 EXPORT_SYMBOL(lprocfs_write_frac_helper);
1564 int lprocfs_read_frac_helper(char *buffer, unsigned long count, long val,
1567 long decimal_val, frac_val;
1573 decimal_val = val / mult;
1574 prtn = snprintf(buffer, count, "%ld", decimal_val);
1575 frac_val = val % mult;
1577 if (prtn < (count - 4) && frac_val > 0) {
1579 int i, temp_mult = 1, frac_bits = 0;
1581 temp_frac = frac_val * 10;
1582 buffer[prtn++] = '.';
1583 while (frac_bits < 2 && (temp_frac / mult) < 1 ) {
1584 /* only reserved 2 bits fraction */
1585 buffer[prtn++] ='0';
1590 * Need to think these cases :
1591 * 1. #echo x.00 > /proc/xxx output result : x
1592 * 2. #echo x.0x > /proc/xxx output result : x.0x
1593 * 3. #echo x.x0 > /proc/xxx output result : x.x
1594 * 4. #echo x.xx > /proc/xxx output result : x.xx
1595 * Only reserved 2 bits fraction.
1597 for (i = 0; i < (5 - prtn); i++)
1600 frac_bits = min((int)count - prtn, 3 - frac_bits);
1601 prtn += snprintf(buffer + prtn, frac_bits, "%ld",
1602 frac_val * temp_mult / mult);
1605 while(buffer[prtn] < '1' || buffer[prtn] > '9') {
1607 if (buffer[prtn] == '.') {
1614 buffer[prtn++] ='\n';
1618 int lprocfs_seq_read_frac_helper(struct seq_file *m, long val, int mult)
1620 long decimal_val, frac_val;
1622 decimal_val = val / mult;
1623 seq_printf(m, "%ld", decimal_val);
1624 frac_val = val % mult;
1631 /* Three cases: x0, xx, 0x */
1632 if ((frac_val % 10) != 0)
1633 seq_printf(m, ".%ld", frac_val);
1635 seq_printf(m, ".%ld", frac_val / 10);
1638 seq_printf(m, "\n");
1641 EXPORT_SYMBOL(lprocfs_seq_read_frac_helper);
1643 int lprocfs_write_u64_helper(const char __user *buffer, unsigned long count,
1646 return lprocfs_write_frac_u64_helper(buffer, count, val, 1);
1648 EXPORT_SYMBOL(lprocfs_write_u64_helper);
1650 int lprocfs_write_frac_u64_helper(const char __user *buffer,
1651 unsigned long count,
1652 __u64 *val, int mult)
1654 char kernbuf[22], *end, *pbuf;
1655 __u64 whole, frac = 0, units;
1656 unsigned frac_d = 1;
1658 if (count > (sizeof(kernbuf) - 1))
1661 if (copy_from_user(kernbuf, buffer, count))
1664 kernbuf[count] = '\0';
1671 whole = simple_strtoull(pbuf, &end, 10);
1675 if (end != NULL && *end == '.') {
1679 /* need to limit frac_d to a __u32 */
1680 if (strlen(pbuf) > 10)
1683 frac = simple_strtoull(pbuf, &end, 10);
1684 /* count decimal places */
1685 for (i = 0; i < (end - pbuf); i++)
1704 /* Specified units override the multiplier */
1706 mult = mult < 0 ? -units : units;
1709 do_div(frac, frac_d);
1710 *val = whole * mult + frac;
1713 EXPORT_SYMBOL(lprocfs_write_frac_u64_helper);
1715 static char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1724 if (!memcmp(s1, s2, l2))
1732 * Find the string \a name in the input \a buffer, and return a pointer to the
1733 * value immediately following \a name, reducing \a count appropriately.
1734 * If \a name is not found the original \a buffer is returned.
1736 char *lprocfs_find_named_value(const char *buffer, const char *name,
1740 size_t buflen = *count;
1742 /* there is no strnstr() in rhel5 and ubuntu kernels */
1743 val = lprocfs_strnstr(buffer, name, buflen);
1745 return (char *)buffer;
1747 val += strlen(name); /* skip prefix */
1748 while (val < buffer + buflen && isspace(*val)) /* skip separator */
1752 while (val < buffer + buflen && isalnum(*val)) {
1757 return val - *count;
1759 EXPORT_SYMBOL(lprocfs_find_named_value);
1761 int lprocfs_seq_create(struct proc_dir_entry *parent,
1764 const struct file_operations *seq_fops,
1767 struct proc_dir_entry *entry;
1770 /* Disallow secretly (un)writable entries. */
1771 LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
1773 entry = proc_create_data(name, mode, parent, seq_fops, data);
1780 EXPORT_SYMBOL(lprocfs_seq_create);
1782 int lprocfs_obd_seq_create(struct obd_device *dev,
1785 const struct file_operations *seq_fops,
1788 return (lprocfs_seq_create(dev->obd_proc_entry, name,
1789 mode, seq_fops, data));
1791 EXPORT_SYMBOL(lprocfs_obd_seq_create);
1793 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
1795 if (value >= OBD_HIST_MAX)
1796 value = OBD_HIST_MAX - 1;
1798 spin_lock(&oh->oh_lock);
1799 oh->oh_buckets[value]++;
1800 spin_unlock(&oh->oh_lock);
1802 EXPORT_SYMBOL(lprocfs_oh_tally);
1804 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
1806 unsigned int val = 0;
1808 if (likely(value != 0))
1809 val = min(fls(value - 1), OBD_HIST_MAX);
1811 lprocfs_oh_tally(oh, val);
1813 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
1815 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
1817 unsigned long ret = 0;
1820 for (i = 0; i < OBD_HIST_MAX; i++)
1821 ret += oh->oh_buckets[i];
1824 EXPORT_SYMBOL(lprocfs_oh_sum);
1826 void lprocfs_oh_clear(struct obd_histogram *oh)
1828 spin_lock(&oh->oh_lock);
1829 memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
1830 spin_unlock(&oh->oh_lock);
1832 EXPORT_SYMBOL(lprocfs_oh_clear);
1834 int lprocfs_obd_rd_max_pages_per_rpc(char *page, char **start, off_t off,
1835 int count, int *eof, void *data)
1837 struct obd_device *dev = data;
1838 struct client_obd *cli = &dev->u.cli;
1841 spin_lock(&cli->cl_loi_list_lock);
1842 rc = snprintf(page, count, "%d\n", cli->cl_max_pages_per_rpc);
1843 spin_unlock(&cli->cl_loi_list_lock);
1848 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
1850 struct obd_device *dev = data;
1851 struct client_obd *cli = &dev->u.cli;
1854 spin_lock(&cli->cl_loi_list_lock);
1855 rc = seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
1856 spin_unlock(&cli->cl_loi_list_lock);
1859 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
1861 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
1862 struct root_squash_info *squash, char *name)
1865 char kernbuf[64], *tmp, *errmsg;
1866 unsigned long uid, gid;
1869 if (count >= sizeof(kernbuf)) {
1870 errmsg = "string too long";
1871 GOTO(failed_noprint, rc = -EINVAL);
1873 if (copy_from_user(kernbuf, buffer, count)) {
1874 errmsg = "bad address";
1875 GOTO(failed_noprint, rc = -EFAULT);
1877 kernbuf[count] = '\0';
1879 /* look for uid gid separator */
1880 tmp = strchr(kernbuf, ':');
1882 errmsg = "needs uid:gid format";
1883 GOTO(failed, rc = -EINVAL);
1889 if (kstrtoul(kernbuf, 0, &uid) != 0) {
1891 GOTO(failed, rc = -EINVAL);
1895 if (kstrtoul(tmp, 0, &gid) != 0) {
1897 GOTO(failed, rc = -EINVAL);
1900 squash->rsi_uid = uid;
1901 squash->rsi_gid = gid;
1903 LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
1904 name, squash->rsi_uid, squash->rsi_gid);
1912 CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
1913 name, kernbuf, errmsg, rc);
1916 CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
1920 EXPORT_SYMBOL(lprocfs_wr_root_squash);
1923 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
1924 struct root_squash_info *squash, char *name)
1927 char *kernbuf = NULL;
1929 struct list_head tmp;
1934 errmsg = "string too long";
1935 GOTO(failed, rc = -EINVAL);
1938 OBD_ALLOC(kernbuf, count + 1);
1939 if (kernbuf == NULL) {
1940 errmsg = "no memory";
1941 GOTO(failed, rc = -ENOMEM);
1943 if (copy_from_user(kernbuf, buffer, count)) {
1944 errmsg = "bad address";
1945 GOTO(failed, rc = -EFAULT);
1947 kernbuf[count] = '\0';
1949 if (count > 0 && kernbuf[count - 1] == '\n')
1952 if ((len == 4 && strncmp(kernbuf, "NONE", len) == 0) ||
1953 (len == 5 && strncmp(kernbuf, "clear", len) == 0)) {
1954 /* empty string is special case */
1955 down_write(&squash->rsi_sem);
1956 if (!list_empty(&squash->rsi_nosquash_nids))
1957 cfs_free_nidlist(&squash->rsi_nosquash_nids);
1958 up_write(&squash->rsi_sem);
1959 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
1960 OBD_FREE(kernbuf, count + 1);
1964 INIT_LIST_HEAD(&tmp);
1965 if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
1966 errmsg = "can't parse";
1967 GOTO(failed, rc = -EINVAL);
1969 LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
1971 OBD_FREE(kernbuf, count + 1);
1974 down_write(&squash->rsi_sem);
1975 if (!list_empty(&squash->rsi_nosquash_nids))
1976 cfs_free_nidlist(&squash->rsi_nosquash_nids);
1977 list_splice(&tmp, &squash->rsi_nosquash_nids);
1978 up_write(&squash->rsi_sem);
1984 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
1985 name, kernbuf, errmsg, rc);
1986 OBD_FREE(kernbuf, count + 1);
1988 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
1993 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
1995 #endif /* CONFIG_PROC_FS*/