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;
326 rc = lprocfs_write_helper(buffer, count, &val);
330 return lprocfs_wr_uint(file, buffer, count, data);
332 EXPORT_SYMBOL(lprocfs_uint_seq_write);
334 int lprocfs_u64_seq_show(struct seq_file *m, void *data)
336 LASSERT(data != NULL);
337 seq_printf(m, LPU64"\n", *(__u64 *)data);
340 EXPORT_SYMBOL(lprocfs_u64_seq_show);
342 int lprocfs_atomic_seq_show(struct seq_file *m, void *data)
344 atomic_t *atom = data;
345 LASSERT(atom != NULL);
346 seq_printf(m, "%d\n", atomic_read(atom));
349 EXPORT_SYMBOL(lprocfs_atomic_seq_show);
352 lprocfs_atomic_seq_write(struct file *file, const char __user *buffer,
353 size_t count, loff_t *off)
355 atomic_t *atm = ((struct seq_file *)file->private_data)->private;
359 rc = lprocfs_write_helper(buffer, count, &val);
366 atomic_set(atm, val);
369 EXPORT_SYMBOL(lprocfs_atomic_seq_write);
371 int lprocfs_uuid_seq_show(struct seq_file *m, void *data)
373 struct obd_device *obd = data;
375 LASSERT(obd != NULL);
376 seq_printf(m, "%s\n", obd->obd_uuid.uuid);
379 EXPORT_SYMBOL(lprocfs_uuid_seq_show);
381 int lprocfs_name_seq_show(struct seq_file *m, void *data)
383 struct obd_device *dev = data;
385 LASSERT(dev != NULL);
386 seq_printf(m, "%s\n", dev->obd_name);
389 EXPORT_SYMBOL(lprocfs_name_seq_show);
391 int lprocfs_blksize_seq_show(struct seq_file *m, void *data)
393 struct obd_device *obd = data;
394 struct obd_statfs osfs;
395 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
396 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
399 seq_printf(m, "%u\n", osfs.os_bsize);
402 EXPORT_SYMBOL(lprocfs_blksize_seq_show);
404 int lprocfs_kbytestotal_seq_show(struct seq_file *m, void *data)
406 struct obd_device *obd = data;
407 struct obd_statfs osfs;
408 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
409 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
412 __u32 blk_size = osfs.os_bsize >> 10;
413 __u64 result = osfs.os_blocks;
415 while (blk_size >>= 1)
418 seq_printf(m, LPU64"\n", result);
422 EXPORT_SYMBOL(lprocfs_kbytestotal_seq_show);
424 int lprocfs_kbytesfree_seq_show(struct seq_file *m, void *data)
426 struct obd_device *obd = data;
427 struct obd_statfs osfs;
428 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
429 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
432 __u32 blk_size = osfs.os_bsize >> 10;
433 __u64 result = osfs.os_bfree;
435 while (blk_size >>= 1)
438 seq_printf(m, LPU64"\n", result);
442 EXPORT_SYMBOL(lprocfs_kbytesfree_seq_show);
444 int lprocfs_kbytesavail_seq_show(struct seq_file *m, void *data)
446 struct obd_device *obd = data;
447 struct obd_statfs osfs;
448 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
449 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
452 __u32 blk_size = osfs.os_bsize >> 10;
453 __u64 result = osfs.os_bavail;
455 while (blk_size >>= 1)
458 seq_printf(m, LPU64"\n", result);
462 EXPORT_SYMBOL(lprocfs_kbytesavail_seq_show);
464 int lprocfs_filestotal_seq_show(struct seq_file *m, void *data)
466 struct obd_device *obd = data;
467 struct obd_statfs osfs;
468 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
469 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
472 seq_printf(m, LPU64"\n", osfs.os_files);
475 EXPORT_SYMBOL(lprocfs_filestotal_seq_show);
477 int lprocfs_filesfree_seq_show(struct seq_file *m, void *data)
479 struct obd_device *obd = data;
480 struct obd_statfs osfs;
481 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
482 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
485 seq_printf(m, LPU64"\n", osfs.os_ffree);
488 EXPORT_SYMBOL(lprocfs_filesfree_seq_show);
490 int lprocfs_server_uuid_seq_show(struct seq_file *m, void *data)
492 struct obd_device *obd = data;
493 struct obd_import *imp;
494 char *imp_state_name = NULL;
497 LASSERT(obd != NULL);
498 LPROCFS_CLIMP_CHECK(obd);
499 imp = obd->u.cli.cl_import;
500 imp_state_name = ptlrpc_import_state_name(imp->imp_state);
501 seq_printf(m, "%s\t%s%s\n", obd2cli_tgt(obd), imp_state_name,
502 imp->imp_deactive ? "\tDEACTIVATED" : "");
504 LPROCFS_CLIMP_EXIT(obd);
507 EXPORT_SYMBOL(lprocfs_server_uuid_seq_show);
509 int lprocfs_conn_uuid_seq_show(struct seq_file *m, void *data)
511 struct obd_device *obd = data;
512 struct ptlrpc_connection *conn;
515 LASSERT(obd != NULL);
517 LPROCFS_CLIMP_CHECK(obd);
518 conn = obd->u.cli.cl_import->imp_connection;
519 if (conn && obd->u.cli.cl_import)
520 seq_printf(m, "%s\n", conn->c_remote_uuid.uuid);
522 seq_printf(m, "%s\n", "<none>");
524 LPROCFS_CLIMP_EXIT(obd);
527 EXPORT_SYMBOL(lprocfs_conn_uuid_seq_show);
529 /** add up per-cpu counters */
530 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
531 struct lprocfs_counter *cnt)
533 unsigned int num_entry;
534 struct lprocfs_counter *percpu_cntr;
536 unsigned long flags = 0;
538 memset(cnt, 0, sizeof(*cnt));
541 /* set count to 1 to avoid divide-by-zero errs in callers */
546 cnt->lc_min = LC_MIN_INIT;
548 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
550 for (i = 0; i < num_entry; i++) {
551 if (stats->ls_percpu[i] == NULL)
553 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
555 cnt->lc_count += percpu_cntr->lc_count;
556 cnt->lc_sum += percpu_cntr->lc_sum;
557 if (percpu_cntr->lc_min < cnt->lc_min)
558 cnt->lc_min = percpu_cntr->lc_min;
559 if (percpu_cntr->lc_max > cnt->lc_max)
560 cnt->lc_max = percpu_cntr->lc_max;
561 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
564 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
568 * Append a space separated list of current set flags to str.
570 #define flag2str(flag) \
572 if (imp->imp_##flag) { \
573 seq_printf(m, "%s" #flag, first ? "" : ", "); \
577 static void obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
581 if (imp->imp_obd->obd_no_recov) {
582 seq_printf(m, "no_recov");
588 flag2str(replayable);
589 flag2str(delayed_recovery);
590 flag2str(no_lock_replay);
591 flag2str(vbr_failed);
593 flag2str(resend_replay);
594 flag2str(no_pinger_recover);
595 flag2str(need_mne_swab);
596 flag2str(connect_tried);
600 static const char *obd_connect_names[] = {
614 "join_file(obsolete)",
618 "remote_client_by_force",
627 "mds_mds_connection",
630 "alt_checksum_algorithm",
668 static void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, char *sep)
674 for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
676 seq_printf(m, "%s%s",
677 first ? "" : sep, obd_connect_names[i]);
681 if (flags & ~(mask - 1))
682 seq_printf(m, "%sunknown_"LPX64,
683 first ? "" : sep, flags & ~(mask - 1));
686 int obd_connect_flags2str(char *page, int count, __u64 flags, char *sep)
691 for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
693 ret += snprintf(page + ret, count - ret, "%s%s",
694 ret ? sep : "", obd_connect_names[i]);
696 if (flags & ~(mask - 1))
697 ret += snprintf(page + ret, count - ret,
699 ret ? sep : "", flags & ~(mask - 1));
702 EXPORT_SYMBOL(obd_connect_flags2str);
704 static void obd_connect_data_seqprint(struct seq_file *m,
705 struct obd_connect_data *ocd)
709 LASSERT(ocd != NULL);
710 flags = ocd->ocd_connect_flags;
712 seq_printf(m, " connect_data:\n"
715 ocd->ocd_connect_flags,
717 if (flags & OBD_CONNECT_VERSION)
718 seq_printf(m, " target_version: %u.%u.%u.%u\n",
719 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
720 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
721 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
722 OBD_OCD_VERSION_FIX(ocd->ocd_version));
723 if (flags & OBD_CONNECT_MDS)
724 seq_printf(m, " mdt_index: %d\n", ocd->ocd_group);
725 if (flags & OBD_CONNECT_GRANT)
726 seq_printf(m, " initial_grant: %d\n", ocd->ocd_grant);
727 if (flags & OBD_CONNECT_INDEX)
728 seq_printf(m, " target_index: %u\n", ocd->ocd_index);
729 if (flags & OBD_CONNECT_BRW_SIZE)
730 seq_printf(m, " max_brw_size: %d\n", ocd->ocd_brw_size);
731 if (flags & OBD_CONNECT_IBITS)
732 seq_printf(m, " ibits_known: "LPX64"\n",
733 ocd->ocd_ibits_known);
734 if (flags & OBD_CONNECT_GRANT_PARAM)
735 seq_printf(m, " grant_block_size: %d\n"
736 " grant_inode_size: %d\n"
737 " grant_max_extent_size: %d\n"
738 " grant_extent_tax: %d\n",
739 1 << ocd->ocd_grant_blkbits,
740 1 << ocd->ocd_grant_inobits,
741 ocd->ocd_grant_max_blks << ocd->ocd_grant_blkbits,
742 ocd->ocd_grant_tax_kb << 10);
743 if (flags & OBD_CONNECT_TRANSNO)
744 seq_printf(m, " first_transno: "LPX64"\n",
746 if (flags & OBD_CONNECT_CKSUM)
747 seq_printf(m, " cksum_types: %#x\n",
748 ocd->ocd_cksum_types);
749 if (flags & OBD_CONNECT_MAX_EASIZE)
750 seq_printf(m, " max_easize: %d\n", ocd->ocd_max_easize);
751 if (flags & OBD_CONNECT_MAXBYTES)
752 seq_printf(m, " max_object_bytes: "LPU64"\n",
754 if (flags & OBD_CONNECT_MULTIMODRPCS)
755 seq_printf(m, " max_mod_rpcs: %hu\n",
756 ocd->ocd_maxmodrpcs);
759 int lprocfs_import_seq_show(struct seq_file *m, void *data)
761 char nidstr[LNET_NIDSTR_SIZE];
762 struct lprocfs_counter ret;
763 struct lprocfs_counter_header *header;
764 struct obd_device *obd = (struct obd_device *)data;
765 struct obd_import *imp;
766 struct obd_import_conn *conn;
767 struct obd_connect_data *ocd;
772 LASSERT(obd != NULL);
773 LPROCFS_CLIMP_CHECK(obd);
774 imp = obd->u.cli.cl_import;
775 ocd = &imp->imp_connect_data;
777 seq_printf(m, "import:\n"
781 " connect_flags: [ ",
784 ptlrpc_import_state_name(imp->imp_state));
785 obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
787 seq_printf(m, " ]\n");
788 obd_connect_data_seqprint(m, ocd);
789 seq_printf(m, " import_flags: [ ");
790 obd_import_flags2str(imp, m);
794 " failover_nids: [ ");
795 spin_lock(&imp->imp_lock);
797 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
798 libcfs_nid2str_r(conn->oic_conn->c_peer.nid,
799 nidstr, sizeof(nidstr));
800 seq_printf(m, "%s%s", j ? ", " : "", nidstr);
803 if (imp->imp_connection != NULL)
804 libcfs_nid2str_r(imp->imp_connection->c_peer.nid,
805 nidstr, sizeof(nidstr));
807 strncpy(nidstr, "<none>", sizeof(nidstr));
809 " current_connection: %s\n"
810 " connection_attempts: %u\n"
812 " in-progress_invalidations: %u\n",
816 atomic_read(&imp->imp_inval_count));
817 spin_unlock(&imp->imp_lock);
819 if (obd->obd_svc_stats == NULL)
822 header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
823 lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
824 if (ret.lc_count != 0) {
825 /* first argument to do_div MUST be __u64 */
826 __u64 sum = ret.lc_sum;
827 do_div(sum, ret.lc_count);
831 seq_printf(m, " rpcs:\n"
833 " unregistering: %u\n"
835 " avg_waittime: "LPU64" %s\n",
836 atomic_read(&imp->imp_inflight),
837 atomic_read(&imp->imp_unregistering),
838 atomic_read(&imp->imp_timeouts),
839 ret.lc_sum, header->lc_units);
842 for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
843 if (imp->imp_at.iat_portal[j] == 0)
845 k = max_t(unsigned int, k,
846 at_get(&imp->imp_at.iat_service_estimate[j]));
848 seq_printf(m, " service_estimates:\n"
849 " services: %u sec\n"
850 " network: %u sec\n",
852 at_get(&imp->imp_at.iat_net_latency));
854 seq_printf(m, " transactions:\n"
855 " last_replay: "LPU64"\n"
856 " peer_committed: "LPU64"\n"
857 " last_checked: "LPU64"\n",
858 imp->imp_last_replay_transno,
859 imp->imp_peer_committed_transno,
860 imp->imp_last_transno_checked);
863 for (rw = 0; rw <= 1; rw++) {
864 lprocfs_stats_collect(obd->obd_svc_stats,
865 PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
867 if (ret.lc_sum > 0 && 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);
872 seq_printf(m, " %s_data_averages:\n"
873 " bytes_per_rpc: "LPU64"\n",
874 rw ? "write" : "read",
878 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
879 header = &obd->obd_svc_stats->ls_cnt_header[j];
880 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
881 if (ret.lc_sum > 0 && ret.lc_count != 0) {
882 /* first argument to do_div MUST be __u64 */
883 __u64 sum = ret.lc_sum;
884 do_div(sum, ret.lc_count);
886 seq_printf(m, " %s_per_rpc: "LPU64"\n",
887 header->lc_units, ret.lc_sum);
890 seq_printf(m, " MB_per_sec: %u.%.02u\n",
891 k / j, (100 * k / j) % 100);
896 LPROCFS_CLIMP_EXIT(obd);
899 EXPORT_SYMBOL(lprocfs_import_seq_show);
901 int lprocfs_state_seq_show(struct seq_file *m, void *data)
903 struct obd_device *obd = (struct obd_device *)data;
904 struct obd_import *imp;
907 LASSERT(obd != NULL);
908 LPROCFS_CLIMP_CHECK(obd);
909 imp = obd->u.cli.cl_import;
911 seq_printf(m, "current_state: %s\n",
912 ptlrpc_import_state_name(imp->imp_state));
913 seq_printf(m, "state_history:\n");
914 k = imp->imp_state_hist_idx;
915 for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
916 struct import_state_hist *ish =
917 &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
918 if (ish->ish_state == 0)
920 seq_printf(m, " - [ "CFS_TIME_T", %s ]\n",
922 ptlrpc_import_state_name(ish->ish_state));
925 LPROCFS_CLIMP_EXIT(obd);
928 EXPORT_SYMBOL(lprocfs_state_seq_show);
930 int lprocfs_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
933 for (i = 0; i < AT_BINS; i++)
934 seq_printf(m, "%3u ", at->at_hist[i]);
938 EXPORT_SYMBOL(lprocfs_at_hist_helper);
940 /* See also ptlrpc_lprocfs_timeouts_show_seq */
941 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
943 struct obd_device *obd = (struct obd_device *)data;
944 struct obd_import *imp;
945 unsigned int cur, worst;
950 LASSERT(obd != NULL);
951 LPROCFS_CLIMP_CHECK(obd);
952 imp = obd->u.cli.cl_import;
954 now = cfs_time_current_sec();
956 /* Some network health info for kicks */
957 s2dhms(&ts, now - imp->imp_last_reply_time);
958 seq_printf(m, "%-10s : %ld, "DHMS_FMT" ago\n",
959 "last reply", imp->imp_last_reply_time, DHMS_VARS(&ts));
961 cur = at_get(&imp->imp_at.iat_net_latency);
962 worst = imp->imp_at.iat_net_latency.at_worst_ever;
963 worstt = imp->imp_at.iat_net_latency.at_worst_time;
964 s2dhms(&ts, now - worstt);
965 seq_printf(m, "%-10s : cur %3u worst %3u (at %ld, "DHMS_FMT" ago) ",
966 "network", cur, worst, worstt, DHMS_VARS(&ts));
967 lprocfs_at_hist_helper(m, &imp->imp_at.iat_net_latency);
969 for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
970 if (imp->imp_at.iat_portal[i] == 0)
972 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
973 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
974 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
975 s2dhms(&ts, now - worstt);
976 seq_printf(m, "portal %-2d : cur %3u worst %3u (at %ld, "
977 DHMS_FMT" ago) ", imp->imp_at.iat_portal[i],
978 cur, worst, worstt, DHMS_VARS(&ts));
979 lprocfs_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
982 LPROCFS_CLIMP_EXIT(obd);
985 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
987 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
989 struct obd_device *obd = data;
992 LPROCFS_CLIMP_CHECK(obd);
993 flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
994 seq_printf(m, "flags="LPX64"\n", flags);
995 obd_connect_seq_flags2str(m, flags, "\n");
997 LPROCFS_CLIMP_EXIT(obd);
1000 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
1003 lprocfs_obd_setup(struct obd_device *obd)
1007 LASSERT(obd != NULL);
1008 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1009 LASSERT(obd->obd_type->typ_procroot != NULL);
1011 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
1012 obd->obd_type->typ_procroot,
1013 obd->obd_vars, obd);
1014 if (IS_ERR(obd->obd_proc_entry)) {
1015 rc = PTR_ERR(obd->obd_proc_entry);
1016 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1017 obd->obd_proc_entry = NULL;
1021 EXPORT_SYMBOL(lprocfs_obd_setup);
1023 int lprocfs_obd_cleanup(struct obd_device *obd)
1027 if (obd->obd_proc_exports_entry) {
1028 /* Should be no exports left */
1029 lprocfs_remove(&obd->obd_proc_exports_entry);
1030 obd->obd_proc_exports_entry = NULL;
1032 if (obd->obd_proc_entry) {
1033 lprocfs_remove(&obd->obd_proc_entry);
1034 obd->obd_proc_entry = NULL;
1038 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1040 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1042 struct lprocfs_counter *cntr;
1043 unsigned int percpusize;
1045 unsigned long flags = 0;
1048 LASSERT(stats->ls_percpu[cpuid] == NULL);
1049 LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1051 percpusize = lprocfs_stats_counter_size(stats);
1052 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1053 if (stats->ls_percpu[cpuid] != NULL) {
1055 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1056 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1057 spin_lock_irqsave(&stats->ls_lock, flags);
1059 spin_lock(&stats->ls_lock);
1060 if (stats->ls_biggest_alloc_num <= cpuid)
1061 stats->ls_biggest_alloc_num = cpuid + 1;
1062 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1063 spin_unlock_irqrestore(&stats->ls_lock, flags);
1065 spin_unlock(&stats->ls_lock);
1068 /* initialize the ls_percpu[cpuid] non-zero counter */
1069 for (i = 0; i < stats->ls_num; ++i) {
1070 cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1071 cntr->lc_min = LC_MIN_INIT;
1076 EXPORT_SYMBOL(lprocfs_stats_alloc_one);
1078 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1079 enum lprocfs_stats_flags flags)
1081 struct lprocfs_stats *stats;
1082 unsigned int num_entry;
1083 unsigned int percpusize = 0;
1089 if (lprocfs_no_percpu_stats != 0)
1090 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1092 if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1095 num_entry = num_possible_cpus();
1097 /* alloc percpu pointers for all possible cpu slots */
1098 LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1102 stats->ls_num = num;
1103 stats->ls_flags = flags;
1104 spin_lock_init(&stats->ls_lock);
1106 /* alloc num of counter headers */
1107 LIBCFS_ALLOC(stats->ls_cnt_header,
1108 stats->ls_num * sizeof(struct lprocfs_counter_header));
1109 if (stats->ls_cnt_header == NULL)
1112 if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1113 /* contains only one set counters */
1114 percpusize = lprocfs_stats_counter_size(stats);
1115 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1116 if (stats->ls_percpu[0] == NULL)
1118 stats->ls_biggest_alloc_num = 1;
1119 } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1120 /* alloc all percpu data, currently only obd_memory use this */
1121 for (i = 0; i < num_entry; ++i)
1122 if (lprocfs_stats_alloc_one(stats, i) < 0)
1129 lprocfs_free_stats(&stats);
1132 EXPORT_SYMBOL(lprocfs_alloc_stats);
1134 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1136 struct lprocfs_stats *stats = *statsh;
1137 unsigned int num_entry;
1138 unsigned int percpusize;
1141 if (stats == NULL || stats->ls_num == 0)
1145 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1148 num_entry = num_possible_cpus();
1150 percpusize = lprocfs_stats_counter_size(stats);
1151 for (i = 0; i < num_entry; i++)
1152 if (stats->ls_percpu[i] != NULL)
1153 LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1154 if (stats->ls_cnt_header != NULL)
1155 LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
1156 sizeof(struct lprocfs_counter_header));
1157 LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1159 EXPORT_SYMBOL(lprocfs_free_stats);
1161 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1163 struct lprocfs_counter *percpu_cntr;
1166 unsigned int num_entry;
1167 unsigned long flags = 0;
1169 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1171 for (i = 0; i < num_entry; i++) {
1172 if (stats->ls_percpu[i] == NULL)
1174 for (j = 0; j < stats->ls_num; j++) {
1175 percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1176 percpu_cntr->lc_count = 0;
1177 percpu_cntr->lc_min = LC_MIN_INIT;
1178 percpu_cntr->lc_max = 0;
1179 percpu_cntr->lc_sumsquare = 0;
1180 percpu_cntr->lc_sum = 0;
1181 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1182 percpu_cntr->lc_sum_irq = 0;
1186 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1188 EXPORT_SYMBOL(lprocfs_clear_stats);
1190 static ssize_t lprocfs_stats_seq_write(struct file *file,
1191 const char __user *buf,
1192 size_t len, loff_t *off)
1194 struct seq_file *seq = file->private_data;
1195 struct lprocfs_stats *stats = seq->private;
1197 lprocfs_clear_stats(stats);
1202 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1204 struct lprocfs_stats *stats = p->private;
1206 return (*pos < stats->ls_num) ? pos : NULL;
1209 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1213 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1217 return lprocfs_stats_seq_start(p, pos);
1220 /* seq file export of one lprocfs counter */
1221 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1223 struct lprocfs_stats *stats = p->private;
1224 struct lprocfs_counter_header *hdr;
1225 struct lprocfs_counter ctr;
1226 int idx = *(loff_t *)v;
1232 do_gettimeofday(&now);
1233 seq_printf(p, "%-25s %lu.%lu secs.usecs\n",
1234 "snapshot_time", now.tv_sec, now.tv_usec);
1239 hdr = &stats->ls_cnt_header[idx];
1240 lprocfs_stats_collect(stats, idx, &ctr);
1242 if (ctr.lc_count == 0)
1245 seq_printf(p, "%-25s "LPD64" samples [%s]", hdr->lc_name,
1246 ctr.lc_count, hdr->lc_units);
1250 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1251 seq_printf(p, " "LPD64" "LPD64" "LPD64,
1252 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1255 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1256 seq_printf(p, " "LPD64, ctr.lc_sumsquare);
1262 return (rc < 0) ? rc : 0;
1265 static const struct seq_operations lprocfs_stats_seq_sops = {
1266 .start = lprocfs_stats_seq_start,
1267 .stop = lprocfs_stats_seq_stop,
1268 .next = lprocfs_stats_seq_next,
1269 .show = lprocfs_stats_seq_show,
1272 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1274 struct seq_file *seq;
1277 rc = LPROCFS_ENTRY_CHECK(inode);
1281 rc = seq_open(file, &lprocfs_stats_seq_sops);
1284 seq = file->private_data;
1285 seq->private = PDE_DATA(inode);
1289 static const struct file_operations lprocfs_stats_seq_fops = {
1290 .owner = THIS_MODULE,
1291 .open = lprocfs_stats_seq_open,
1293 .write = lprocfs_stats_seq_write,
1294 .llseek = seq_lseek,
1295 .release = lprocfs_seq_release,
1298 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1299 struct lprocfs_stats *stats)
1301 struct proc_dir_entry *entry;
1302 LASSERT(root != NULL);
1304 entry = proc_create_data(name, 0644, root,
1305 &lprocfs_stats_seq_fops, stats);
1310 EXPORT_SYMBOL(lprocfs_register_stats);
1312 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1313 unsigned conf, const char *name, const char *units)
1315 struct lprocfs_counter_header *header;
1316 struct lprocfs_counter *percpu_cntr;
1317 unsigned long flags = 0;
1319 unsigned int num_cpu;
1321 LASSERT(stats != NULL);
1323 header = &stats->ls_cnt_header[index];
1324 LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1325 index, name, units);
1327 header->lc_config = conf;
1328 header->lc_name = name;
1329 header->lc_units = units;
1331 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1332 for (i = 0; i < num_cpu; ++i) {
1333 if (stats->ls_percpu[i] == NULL)
1335 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1336 percpu_cntr->lc_count = 0;
1337 percpu_cntr->lc_min = LC_MIN_INIT;
1338 percpu_cntr->lc_max = 0;
1339 percpu_cntr->lc_sumsquare = 0;
1340 percpu_cntr->lc_sum = 0;
1341 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1342 percpu_cntr->lc_sum_irq = 0;
1344 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1346 EXPORT_SYMBOL(lprocfs_counter_init);
1348 /* Note that we only init md counters for ops whose offset is less
1349 * than NUM_MD_STATS. This is explained in a comment in the definition
1350 * of struct md_ops. */
1351 #define LPROCFS_MD_OP_INIT(base, stats, op) \
1353 unsigned int _idx = base + MD_COUNTER_OFFSET(op); \
1355 if (MD_COUNTER_OFFSET(op) < NUM_MD_STATS) { \
1356 LASSERT(_idx < stats->ls_num); \
1357 lprocfs_counter_init(stats, _idx, 0, #op, "reqs"); \
1361 void lprocfs_init_mps_stats(int num_private_stats, struct lprocfs_stats *stats)
1363 LPROCFS_MD_OP_INIT(num_private_stats, stats, getstatus);
1364 LPROCFS_MD_OP_INIT(num_private_stats, stats, null_inode);
1365 LPROCFS_MD_OP_INIT(num_private_stats, stats, close);
1366 LPROCFS_MD_OP_INIT(num_private_stats, stats, create);
1367 LPROCFS_MD_OP_INIT(num_private_stats, stats, enqueue);
1368 LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr);
1369 LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr_name);
1370 LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_lock);
1371 LPROCFS_MD_OP_INIT(num_private_stats, stats, link);
1372 LPROCFS_MD_OP_INIT(num_private_stats, stats, rename);
1373 LPROCFS_MD_OP_INIT(num_private_stats, stats, setattr);
1374 LPROCFS_MD_OP_INIT(num_private_stats, stats, fsync);
1375 LPROCFS_MD_OP_INIT(num_private_stats, stats, read_page);
1376 LPROCFS_MD_OP_INIT(num_private_stats, stats, unlink);
1377 LPROCFS_MD_OP_INIT(num_private_stats, stats, setxattr);
1378 LPROCFS_MD_OP_INIT(num_private_stats, stats, getxattr);
1379 LPROCFS_MD_OP_INIT(num_private_stats, stats, init_ea_size);
1380 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_lustre_md);
1381 LPROCFS_MD_OP_INIT(num_private_stats, stats, free_lustre_md);
1382 LPROCFS_MD_OP_INIT(num_private_stats, stats, merge_attr);
1383 LPROCFS_MD_OP_INIT(num_private_stats, stats, set_open_replay_data);
1384 LPROCFS_MD_OP_INIT(num_private_stats, stats, clear_open_replay_data);
1385 LPROCFS_MD_OP_INIT(num_private_stats, stats, set_lock_data);
1386 LPROCFS_MD_OP_INIT(num_private_stats, stats, lock_match);
1387 LPROCFS_MD_OP_INIT(num_private_stats, stats, cancel_unused);
1388 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_remote_perm);
1389 LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_getattr_async);
1390 LPROCFS_MD_OP_INIT(num_private_stats, stats, revalidate_lock);
1393 int lprocfs_alloc_md_stats(struct obd_device *obd,
1394 unsigned int num_private_stats)
1396 struct lprocfs_stats *stats;
1397 unsigned int num_stats;
1400 CLASSERT(offsetof(struct md_ops, MD_STATS_FIRST_OP) == 0);
1401 CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_FIRST_OP) == 0);
1402 CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_LAST_OP) > 0);
1404 /* TODO Ensure that this function is only used where
1405 * appropriate by adding an assertion to the effect that
1406 * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1407 * because mdt_procfs_init() uses this function to allocate
1408 * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1409 * mdt layer does not use the md_ops interface. This is
1410 * confusing and a waste of memory. See LU-2484.
1412 LASSERT(obd->obd_proc_entry != NULL);
1413 LASSERT(obd->obd_md_stats == NULL);
1414 LASSERT(obd->obd_md_cntr_base == 0);
1416 num_stats = NUM_MD_STATS + num_private_stats;
1417 stats = lprocfs_alloc_stats(num_stats, 0);
1421 lprocfs_init_mps_stats(num_private_stats, stats);
1423 for (i = num_private_stats; i < num_stats; i++) {
1424 if (stats->ls_cnt_header[i].lc_name == NULL) {
1425 CERROR("Missing md_stat initializer md_op "
1426 "operation at offset %d. Aborting.\n",
1427 i - num_private_stats);
1432 rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1434 lprocfs_free_stats(&stats);
1436 obd->obd_md_stats = stats;
1437 obd->obd_md_cntr_base = num_private_stats;
1442 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1444 void lprocfs_free_md_stats(struct obd_device *obd)
1446 struct lprocfs_stats *stats = obd->obd_md_stats;
1448 if (stats != NULL) {
1449 obd->obd_md_stats = NULL;
1450 obd->obd_md_cntr_base = 0;
1451 lprocfs_free_stats(&stats);
1454 EXPORT_SYMBOL(lprocfs_free_md_stats);
1456 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1458 lprocfs_counter_init(ldlm_stats,
1459 LDLM_ENQUEUE - LDLM_FIRST_OPC,
1460 0, "ldlm_enqueue", "reqs");
1461 lprocfs_counter_init(ldlm_stats,
1462 LDLM_CONVERT - LDLM_FIRST_OPC,
1463 0, "ldlm_convert", "reqs");
1464 lprocfs_counter_init(ldlm_stats,
1465 LDLM_CANCEL - LDLM_FIRST_OPC,
1466 0, "ldlm_cancel", "reqs");
1467 lprocfs_counter_init(ldlm_stats,
1468 LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1469 0, "ldlm_bl_callback", "reqs");
1470 lprocfs_counter_init(ldlm_stats,
1471 LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1472 0, "ldlm_cp_callback", "reqs");
1473 lprocfs_counter_init(ldlm_stats,
1474 LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1475 0, "ldlm_gl_callback", "reqs");
1477 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1479 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1480 struct lprocfs_counter_header *header,
1481 enum lprocfs_stats_flags flags,
1482 enum lprocfs_fields_flags field)
1486 if (lc == NULL || header == NULL)
1490 case LPROCFS_FIELDS_FLAGS_CONFIG:
1491 ret = header->lc_config;
1493 case LPROCFS_FIELDS_FLAGS_SUM:
1495 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1496 ret += lc->lc_sum_irq;
1498 case LPROCFS_FIELDS_FLAGS_MIN:
1501 case LPROCFS_FIELDS_FLAGS_MAX:
1504 case LPROCFS_FIELDS_FLAGS_AVG:
1505 ret = (lc->lc_max - lc->lc_min) / 2;
1507 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1508 ret = lc->lc_sumsquare;
1510 case LPROCFS_FIELDS_FLAGS_COUNT:
1518 EXPORT_SYMBOL(lprocfs_read_helper);
1520 int lprocfs_write_helper(const char __user *buffer, unsigned long count,
1523 return lprocfs_write_frac_helper(buffer, count, val, 1);
1525 EXPORT_SYMBOL(lprocfs_write_helper);
1527 int lprocfs_write_frac_helper(const char __user *buffer, unsigned long count,
1530 char kernbuf[20], *end, *pbuf;
1532 if (count > (sizeof(kernbuf) - 1))
1535 if (copy_from_user(kernbuf, buffer, count))
1538 kernbuf[count] = '\0';
1545 *val = (int)simple_strtoul(pbuf, &end, 10) * mult;
1549 if (end != NULL && *end == '.') {
1550 int temp_val, pow = 1;
1554 if (strlen(pbuf) > 5)
1555 pbuf[5] = '\0'; /*only allow 5bits fractional*/
1557 temp_val = (int)simple_strtoul(pbuf, &end, 10) * mult;
1560 for (i = 0; i < (end - pbuf); i++)
1563 *val += temp_val / pow;
1568 EXPORT_SYMBOL(lprocfs_write_frac_helper);
1570 int lprocfs_read_frac_helper(char *buffer, unsigned long count, long val,
1573 long decimal_val, frac_val;
1579 decimal_val = val / mult;
1580 prtn = snprintf(buffer, count, "%ld", decimal_val);
1581 frac_val = val % mult;
1583 if (prtn < (count - 4) && frac_val > 0) {
1585 int i, temp_mult = 1, frac_bits = 0;
1587 temp_frac = frac_val * 10;
1588 buffer[prtn++] = '.';
1589 while (frac_bits < 2 && (temp_frac / mult) < 1 ) {
1590 /* only reserved 2 bits fraction */
1591 buffer[prtn++] ='0';
1596 * Need to think these cases :
1597 * 1. #echo x.00 > /proc/xxx output result : x
1598 * 2. #echo x.0x > /proc/xxx output result : x.0x
1599 * 3. #echo x.x0 > /proc/xxx output result : x.x
1600 * 4. #echo x.xx > /proc/xxx output result : x.xx
1601 * Only reserved 2 bits fraction.
1603 for (i = 0; i < (5 - prtn); i++)
1606 frac_bits = min((int)count - prtn, 3 - frac_bits);
1607 prtn += snprintf(buffer + prtn, frac_bits, "%ld",
1608 frac_val * temp_mult / mult);
1611 while(buffer[prtn] < '1' || buffer[prtn] > '9') {
1613 if (buffer[prtn] == '.') {
1620 buffer[prtn++] ='\n';
1624 int lprocfs_seq_read_frac_helper(struct seq_file *m, long val, int mult)
1626 long decimal_val, frac_val;
1628 decimal_val = val / mult;
1629 seq_printf(m, "%ld", decimal_val);
1630 frac_val = val % mult;
1637 /* Three cases: x0, xx, 0x */
1638 if ((frac_val % 10) != 0)
1639 seq_printf(m, ".%ld", frac_val);
1641 seq_printf(m, ".%ld", frac_val / 10);
1644 seq_printf(m, "\n");
1647 EXPORT_SYMBOL(lprocfs_seq_read_frac_helper);
1649 int lprocfs_write_u64_helper(const char __user *buffer, unsigned long count,
1652 return lprocfs_write_frac_u64_helper(buffer, count, val, 1);
1654 EXPORT_SYMBOL(lprocfs_write_u64_helper);
1656 int lprocfs_write_frac_u64_helper(const char __user *buffer,
1657 unsigned long count,
1658 __u64 *val, int mult)
1660 char kernbuf[22], *end, *pbuf;
1661 __u64 whole, frac = 0, units;
1662 unsigned frac_d = 1;
1664 if (count > (sizeof(kernbuf) - 1))
1667 if (copy_from_user(kernbuf, buffer, count))
1670 kernbuf[count] = '\0';
1677 whole = simple_strtoull(pbuf, &end, 10);
1681 if (end != NULL && *end == '.') {
1685 /* need to limit frac_d to a __u32 */
1686 if (strlen(pbuf) > 10)
1689 frac = simple_strtoull(pbuf, &end, 10);
1690 /* count decimal places */
1691 for (i = 0; i < (end - pbuf); i++)
1710 /* Specified units override the multiplier */
1712 mult = mult < 0 ? -units : units;
1715 do_div(frac, frac_d);
1716 *val = whole * mult + frac;
1719 EXPORT_SYMBOL(lprocfs_write_frac_u64_helper);
1721 static char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1730 if (!memcmp(s1, s2, l2))
1738 * Find the string \a name in the input \a buffer, and return a pointer to the
1739 * value immediately following \a name, reducing \a count appropriately.
1740 * If \a name is not found the original \a buffer is returned.
1742 char *lprocfs_find_named_value(const char *buffer, const char *name,
1746 size_t buflen = *count;
1748 /* there is no strnstr() in rhel5 and ubuntu kernels */
1749 val = lprocfs_strnstr(buffer, name, buflen);
1751 return (char *)buffer;
1753 val += strlen(name); /* skip prefix */
1754 while (val < buffer + buflen && isspace(*val)) /* skip separator */
1758 while (val < buffer + buflen && isalnum(*val)) {
1763 return val - *count;
1765 EXPORT_SYMBOL(lprocfs_find_named_value);
1767 int lprocfs_seq_create(struct proc_dir_entry *parent,
1770 const struct file_operations *seq_fops,
1773 struct proc_dir_entry *entry;
1776 /* Disallow secretly (un)writable entries. */
1777 LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
1779 entry = proc_create_data(name, mode, parent, seq_fops, data);
1786 EXPORT_SYMBOL(lprocfs_seq_create);
1788 int lprocfs_obd_seq_create(struct obd_device *dev,
1791 const struct file_operations *seq_fops,
1794 return (lprocfs_seq_create(dev->obd_proc_entry, name,
1795 mode, seq_fops, data));
1797 EXPORT_SYMBOL(lprocfs_obd_seq_create);
1799 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
1801 if (value >= OBD_HIST_MAX)
1802 value = OBD_HIST_MAX - 1;
1804 spin_lock(&oh->oh_lock);
1805 oh->oh_buckets[value]++;
1806 spin_unlock(&oh->oh_lock);
1808 EXPORT_SYMBOL(lprocfs_oh_tally);
1810 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
1812 unsigned int val = 0;
1814 if (likely(value != 0))
1815 val = min(fls(value - 1), OBD_HIST_MAX);
1817 lprocfs_oh_tally(oh, val);
1819 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
1821 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
1823 unsigned long ret = 0;
1826 for (i = 0; i < OBD_HIST_MAX; i++)
1827 ret += oh->oh_buckets[i];
1830 EXPORT_SYMBOL(lprocfs_oh_sum);
1832 void lprocfs_oh_clear(struct obd_histogram *oh)
1834 spin_lock(&oh->oh_lock);
1835 memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
1836 spin_unlock(&oh->oh_lock);
1838 EXPORT_SYMBOL(lprocfs_oh_clear);
1840 int lprocfs_obd_rd_max_pages_per_rpc(char *page, char **start, off_t off,
1841 int count, int *eof, void *data)
1843 struct obd_device *dev = data;
1844 struct client_obd *cli = &dev->u.cli;
1847 spin_lock(&cli->cl_loi_list_lock);
1848 rc = snprintf(page, count, "%d\n", cli->cl_max_pages_per_rpc);
1849 spin_unlock(&cli->cl_loi_list_lock);
1854 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
1856 struct obd_device *dev = data;
1857 struct client_obd *cli = &dev->u.cli;
1859 spin_lock(&cli->cl_loi_list_lock);
1860 seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
1861 spin_unlock(&cli->cl_loi_list_lock);
1864 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
1866 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
1867 struct root_squash_info *squash, char *name)
1870 char kernbuf[64], *tmp, *errmsg;
1871 unsigned long uid, gid;
1874 if (count >= sizeof(kernbuf)) {
1875 errmsg = "string too long";
1876 GOTO(failed_noprint, rc = -EINVAL);
1878 if (copy_from_user(kernbuf, buffer, count)) {
1879 errmsg = "bad address";
1880 GOTO(failed_noprint, rc = -EFAULT);
1882 kernbuf[count] = '\0';
1884 /* look for uid gid separator */
1885 tmp = strchr(kernbuf, ':');
1887 errmsg = "needs uid:gid format";
1888 GOTO(failed, rc = -EINVAL);
1894 if (kstrtoul(kernbuf, 0, &uid) != 0) {
1896 GOTO(failed, rc = -EINVAL);
1900 if (kstrtoul(tmp, 0, &gid) != 0) {
1902 GOTO(failed, rc = -EINVAL);
1905 squash->rsi_uid = uid;
1906 squash->rsi_gid = gid;
1908 LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
1909 name, squash->rsi_uid, squash->rsi_gid);
1917 CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
1918 name, kernbuf, errmsg, rc);
1921 CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
1925 EXPORT_SYMBOL(lprocfs_wr_root_squash);
1928 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
1929 struct root_squash_info *squash, char *name)
1932 char *kernbuf = NULL;
1934 struct list_head tmp;
1939 errmsg = "string too long";
1940 GOTO(failed, rc = -EINVAL);
1943 OBD_ALLOC(kernbuf, count + 1);
1944 if (kernbuf == NULL) {
1945 errmsg = "no memory";
1946 GOTO(failed, rc = -ENOMEM);
1948 if (copy_from_user(kernbuf, buffer, count)) {
1949 errmsg = "bad address";
1950 GOTO(failed, rc = -EFAULT);
1952 kernbuf[count] = '\0';
1954 if (count > 0 && kernbuf[count - 1] == '\n')
1957 if ((len == 4 && strncmp(kernbuf, "NONE", len) == 0) ||
1958 (len == 5 && strncmp(kernbuf, "clear", len) == 0)) {
1959 /* empty string is special case */
1960 down_write(&squash->rsi_sem);
1961 if (!list_empty(&squash->rsi_nosquash_nids))
1962 cfs_free_nidlist(&squash->rsi_nosquash_nids);
1963 up_write(&squash->rsi_sem);
1964 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
1965 OBD_FREE(kernbuf, count + 1);
1969 INIT_LIST_HEAD(&tmp);
1970 if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
1971 errmsg = "can't parse";
1972 GOTO(failed, rc = -EINVAL);
1974 LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
1976 OBD_FREE(kernbuf, count + 1);
1979 down_write(&squash->rsi_sem);
1980 if (!list_empty(&squash->rsi_nosquash_nids))
1981 cfs_free_nidlist(&squash->rsi_nosquash_nids);
1982 list_splice(&tmp, &squash->rsi_nosquash_nids);
1983 up_write(&squash->rsi_sem);
1989 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
1990 name, kernbuf, errmsg, rc);
1991 OBD_FREE(kernbuf, count + 1);
1993 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
1998 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);
2000 #endif /* CONFIG_PROC_FS*/