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, 2013, 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
44 #include <obd_class.h>
45 #include <lprocfs_status.h>
46 #include <lustre/lustre_idl.h>
50 static int lprocfs_no_percpu_stats = 0;
51 CFS_MODULE_PARM(lprocfs_no_percpu_stats, "i", int, 0644,
52 "Do not alloc percpu data for lprocfs stats");
54 #define MAX_STRING_SIZE 128
56 int lprocfs_single_release(struct inode *inode, struct file *file)
58 return single_release(inode, file);
60 EXPORT_SYMBOL(lprocfs_single_release);
62 int lprocfs_seq_release(struct inode *inode, struct file *file)
64 return seq_release(inode, file);
66 EXPORT_SYMBOL(lprocfs_seq_release);
68 struct proc_dir_entry *
69 lprocfs_add_simple(struct proc_dir_entry *root, char *name,
70 #ifndef HAVE_ONLY_PROCFS_SEQ
71 read_proc_t *read_proc, write_proc_t *write_proc,
73 void *data, const struct file_operations *fops)
75 struct proc_dir_entry *proc;
78 if (root == NULL || name == NULL)
79 return ERR_PTR(-EINVAL);
82 #ifndef HAVE_ONLY_PROCFS_SEQ
88 LPROCFS_WRITE_ENTRY();
89 proc = create_proc_entry(name, mode, root);
91 CERROR("LprocFS: No memory to create /proc entry %s\n",
94 return ERR_PTR(-ENOMEM);
96 proc->read_proc = read_proc;
97 proc->write_proc = write_proc;
101 return ERR_PTR(-EINVAL);
108 proc = proc_create_data(name, mode, root, fops, data);
110 CERROR("LprocFS: No memory to create /proc entry %s\n",
112 return ERR_PTR(-ENOMEM);
117 EXPORT_SYMBOL(lprocfs_add_simple);
119 struct proc_dir_entry *lprocfs_add_symlink(const char *name,
120 struct proc_dir_entry *parent, const char *format, ...)
122 struct proc_dir_entry *entry;
126 if (parent == NULL || format == NULL)
129 OBD_ALLOC_WAIT(dest, MAX_STRING_SIZE + 1);
133 va_start(ap, format);
134 vsnprintf(dest, MAX_STRING_SIZE, format, ap);
137 entry = proc_symlink(name, parent, dest);
139 CERROR("LprocFS: Could not create symbolic link from "
140 "%s to %s\n", name, dest);
142 OBD_FREE(dest, MAX_STRING_SIZE + 1);
145 EXPORT_SYMBOL(lprocfs_add_symlink);
147 #ifdef HAVE_ONLY_PROCFS_SEQ
148 static const struct file_operations lprocfs_generic_fops = { };
152 lprocfs_fops_read(struct file *f, char __user *buf, size_t size, loff_t *ppos)
154 struct proc_dir_entry *dp = PDE(f->f_dentry->d_inode);
155 char *page, *start = NULL;
156 int rc = 0, eof = 1, count;
158 if (*ppos >= PAGE_CACHE_SIZE)
161 page = (char *)__get_free_page(GFP_KERNEL);
165 if (LPROCFS_ENTRY_CHECK(dp)) {
170 OBD_FAIL_TIMEOUT(OBD_FAIL_LPROC_REMOVE, 10);
172 rc = dp->read_proc(page, &start, *ppos, PAGE_CACHE_SIZE,
177 /* for lustre proc read, the read count must be less than PAGE_SIZE */
186 start = page + *ppos;
187 } else if (start < page) {
191 count = (rc < size) ? rc : size;
192 if (copy_to_user(buf, start, count)) {
199 free_page((unsigned long)page);
204 lprocfs_fops_write(struct file *f, const char __user *buf, size_t size,
207 struct proc_dir_entry *dp = PDE(f->f_dentry->d_inode);
210 if (LPROCFS_ENTRY_CHECK(dp))
213 rc = dp->write_proc(f, buf, size, dp->data);
217 static struct file_operations lprocfs_generic_fops = {
218 .owner = THIS_MODULE,
219 .read = lprocfs_fops_read,
220 .write = lprocfs_fops_write,
223 /* for b=10866, global variable */
224 DECLARE_RWSEM(_lprocfs_lock);
225 EXPORT_SYMBOL(_lprocfs_lock);
227 static struct proc_dir_entry *__lprocfs_srch(struct proc_dir_entry *head,
230 struct proc_dir_entry *temp;
236 while (temp != NULL) {
237 if (strcmp(temp->name, name) == 0)
244 struct proc_dir_entry *lprocfs_srch(struct proc_dir_entry *head,
247 struct proc_dir_entry *temp;
249 LPROCFS_SRCH_ENTRY();
250 temp = __lprocfs_srch(head, name);
254 EXPORT_SYMBOL(lprocfs_srch);
256 static int __lprocfs_add_vars(struct proc_dir_entry *root,
257 struct lprocfs_vars *list,
262 if (root == NULL || list == NULL)
265 while (list->name != NULL) {
266 struct proc_dir_entry *cur_root, *proc;
267 char *pathcopy, *cur, *next, pathbuf[64];
268 int pathsize = strlen(list->name) + 1;
273 /* need copy of path for strsep */
274 if (strlen(list->name) > sizeof(pathbuf) - 1) {
275 OBD_ALLOC(pathcopy, pathsize);
276 if (pathcopy == NULL)
277 GOTO(out, rc = -ENOMEM);
283 strcpy(pathcopy, list->name);
285 while (cur_root != NULL && (cur = strsep(&next, "/"))) {
286 if (*cur =='\0') /* skip double/trailing "/" */
289 proc = __lprocfs_srch(cur_root, cur);
290 CDEBUG(D_OTHER, "cur_root=%s, cur=%s, next=%s, (%s)\n",
291 cur_root->name, cur, next,
292 (proc ? "exists" : "new"));
294 cur_root = (proc ? proc :
295 proc_mkdir(cur, cur_root));
296 } else if (proc == NULL) {
298 if (list->proc_mode != 0000) {
299 mode = list->proc_mode;
303 if (list->write_fptr)
306 proc = create_proc_entry(cur, mode, cur_root);
310 if (pathcopy != pathbuf)
311 OBD_FREE(pathcopy, pathsize);
313 if (cur_root == NULL || proc == NULL) {
314 CERROR("LprocFS: No memory to create /proc entry %s\n",
316 GOTO(out, rc = -ENOMEM);
320 proc->proc_fops = list->fops;
322 proc->proc_fops = &lprocfs_generic_fops;
323 proc->read_proc = list->read_fptr;
324 proc->write_proc = list->write_fptr;
325 proc->data = (list->data ? list->data : data);
332 int lprocfs_add_vars(struct proc_dir_entry *root, struct lprocfs_vars *list,
337 LPROCFS_WRITE_ENTRY();
338 rc = __lprocfs_add_vars(root, list, data);
339 LPROCFS_WRITE_EXIT();
343 EXPORT_SYMBOL(lprocfs_add_vars);
349 * \param root [in] The parent proc entry on which new entry will be added.
350 * \param list [in] Array of proc entries to be added.
351 * \param data [in] The argument to be passed when entries read/write routines
352 * are called through /proc file.
354 * \retval 0 on success
358 lprocfs_seq_add_vars(struct proc_dir_entry *root, struct lprocfs_seq_vars *list,
361 if (root == NULL || list == NULL)
364 while (list->name != NULL) {
365 struct proc_dir_entry *proc;
368 if (list->proc_mode != 0000) {
369 mode = list->proc_mode;
370 } else if (list->fops) {
371 if (list->fops->read)
373 if (list->fops->write)
376 proc = proc_create_data(list->name, mode, root,
377 list->fops ?: &lprocfs_generic_fops,
385 EXPORT_SYMBOL(lprocfs_seq_add_vars);
387 #ifndef HAVE_ONLY_PROCFS_SEQ
388 void lprocfs_remove_nolock(struct proc_dir_entry **proot)
390 struct proc_dir_entry *root = *proot;
391 struct proc_dir_entry *temp = root;
392 struct proc_dir_entry *rm_entry;
393 struct proc_dir_entry *parent;
396 if (root == NULL || IS_ERR(root))
399 parent = root->parent;
400 LASSERT(parent != NULL);
403 while (temp->subdir != NULL)
409 /* Memory corruption once caused this to fail, and
410 without this LASSERT we would loop here forever. */
411 LASSERTF(strlen(rm_entry->name) == rm_entry->namelen,
412 "0x%p %s/%s len %d\n", rm_entry, temp->name,
413 rm_entry->name, (int)strlen(rm_entry->name));
415 remove_proc_entry(rm_entry->name, temp);
422 void lprocfs_remove(struct proc_dir_entry **rooth)
424 #ifndef HAVE_ONLY_PROCFS_SEQ
425 LPROCFS_WRITE_ENTRY(); /* search vs remove race */
426 lprocfs_remove_nolock(rooth);
427 LPROCFS_WRITE_EXIT();
433 EXPORT_SYMBOL(lprocfs_remove);
435 void lprocfs_remove_proc_entry(const char *name, struct proc_dir_entry *parent)
437 LASSERT(parent != NULL);
438 remove_proc_entry(name, parent);
440 EXPORT_SYMBOL(lprocfs_remove_proc_entry);
442 #ifndef HAVE_ONLY_PROCFS_SEQ
443 void lprocfs_try_remove_proc_entry(const char *name,
444 struct proc_dir_entry *parent)
446 struct proc_dir_entry *t = NULL;
447 struct proc_dir_entry **p;
450 LASSERT(parent != NULL);
453 LPROCFS_WRITE_ENTRY();
455 /* lookup target name */
456 for (p = &parent->subdir; *p; p = &(*p)->next) {
457 if ((*p)->namelen != len)
459 if (memcmp(name, (*p)->name, len))
466 /* verify it's empty: do not count "num_refs" */
467 for (p = &t->subdir; *p; p = &(*p)->next) {
468 if ((*p)->namelen != strlen("num_refs")) {
472 if (memcmp("num_refs", (*p)->name,
473 strlen("num_refs"))) {
481 lprocfs_remove_nolock(&t);
483 LPROCFS_WRITE_EXIT();
487 EXPORT_SYMBOL(lprocfs_try_remove_proc_entry);
489 struct proc_dir_entry *lprocfs_register(const char *name,
490 struct proc_dir_entry *parent,
491 struct lprocfs_vars *list, void *data)
493 struct proc_dir_entry *entry;
496 LPROCFS_WRITE_ENTRY();
497 entry = __lprocfs_srch(parent, name);
499 CERROR("entry '%s' already registered\n", name);
500 GOTO(out, entry = ERR_PTR(-EALREADY));
503 entry = proc_mkdir(name, parent);
505 GOTO(out, entry = ERR_PTR(-ENOMEM));
508 rc = __lprocfs_add_vars(entry, list, data);
510 lprocfs_remove_nolock(&entry);
511 GOTO(out, entry = ERR_PTR(rc));
515 LPROCFS_WRITE_EXIT();
518 EXPORT_SYMBOL(lprocfs_register);
521 struct proc_dir_entry *
522 lprocfs_seq_register(const char *name, struct proc_dir_entry *parent,
523 struct lprocfs_seq_vars *list, void *data)
525 struct proc_dir_entry *newchild;
527 newchild = proc_mkdir(name, parent);
528 if (newchild != NULL && list != NULL) {
529 int rc = lprocfs_seq_add_vars(newchild, list, data);
531 lprocfs_remove(&newchild);
537 EXPORT_SYMBOL(lprocfs_seq_register);
539 /* Generic callbacks */
540 int lprocfs_uint_seq_show(struct seq_file *m, void *data)
542 return seq_printf(m, "%u\n", *(unsigned int *)data);
544 EXPORT_SYMBOL(lprocfs_uint_seq_show);
546 int lprocfs_wr_uint(struct file *file, const char *buffer,
547 unsigned long count, void *data)
550 char dummy[MAX_STRING_SIZE + 1], *end;
553 dummy[MAX_STRING_SIZE] = '\0';
554 if (copy_from_user(dummy, buffer, MAX_STRING_SIZE))
557 tmp = simple_strtoul(dummy, &end, 0);
561 *p = (unsigned int)tmp;
564 EXPORT_SYMBOL(lprocfs_wr_uint);
566 ssize_t lprocfs_uint_seq_write(struct file *file, const char *buffer,
567 size_t count, loff_t *off)
569 int *data = ((struct seq_file *)file->private_data)->private;
572 rc = lprocfs_write_helper(buffer, count, &val);
576 return lprocfs_wr_uint(file, buffer, count, data);
578 EXPORT_SYMBOL(lprocfs_uint_seq_write);
580 int lprocfs_u64_seq_show(struct seq_file *m, void *data)
582 LASSERT(data != NULL);
583 return seq_printf(m, LPU64"\n", *(__u64 *)data);
585 EXPORT_SYMBOL(lprocfs_u64_seq_show);
587 int lprocfs_atomic_seq_show(struct seq_file *m, void *data)
589 atomic_t *atom = data;
590 LASSERT(atom != NULL);
591 return seq_printf(m, "%d\n", atomic_read(atom));
593 EXPORT_SYMBOL(lprocfs_atomic_seq_show);
596 lprocfs_atomic_seq_write(struct file *file, const char *buffer,
597 size_t count, loff_t *off)
599 atomic_t *atm = ((struct seq_file *)file->private_data)->private;
603 rc = lprocfs_write_helper(buffer, count, &val);
610 atomic_set(atm, val);
613 EXPORT_SYMBOL(lprocfs_atomic_seq_write);
615 int lprocfs_uuid_seq_show(struct seq_file *m, void *data)
617 struct obd_device *obd = data;
619 LASSERT(obd != NULL);
620 return seq_printf(m, "%s\n", obd->obd_uuid.uuid);
622 EXPORT_SYMBOL(lprocfs_uuid_seq_show);
624 int lprocfs_name_seq_show(struct seq_file *m, void *data)
626 struct obd_device *dev = data;
628 LASSERT(dev != NULL);
629 return seq_printf(m, "%s\n", dev->obd_name);
631 EXPORT_SYMBOL(lprocfs_name_seq_show);
633 int lprocfs_blksize_seq_show(struct seq_file *m, void *data)
635 struct obd_device *obd = data;
636 struct obd_statfs osfs;
637 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
638 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
641 rc = seq_printf(m, "%u\n", osfs.os_bsize);
644 EXPORT_SYMBOL(lprocfs_blksize_seq_show);
646 int lprocfs_kbytestotal_seq_show(struct seq_file *m, void *data)
648 struct obd_device *obd = data;
649 struct obd_statfs osfs;
650 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
651 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
654 __u32 blk_size = osfs.os_bsize >> 10;
655 __u64 result = osfs.os_blocks;
657 while (blk_size >>= 1)
660 rc = seq_printf(m, LPU64"\n", result);
664 EXPORT_SYMBOL(lprocfs_kbytestotal_seq_show);
666 int lprocfs_kbytesfree_seq_show(struct seq_file *m, void *data)
668 struct obd_device *obd = data;
669 struct obd_statfs osfs;
670 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
671 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
674 __u32 blk_size = osfs.os_bsize >> 10;
675 __u64 result = osfs.os_bfree;
677 while (blk_size >>= 1)
680 rc = seq_printf(m, LPU64"\n", result);
684 EXPORT_SYMBOL(lprocfs_kbytesfree_seq_show);
686 int lprocfs_kbytesavail_seq_show(struct seq_file *m, void *data)
688 struct obd_device *obd = data;
689 struct obd_statfs osfs;
690 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
691 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
694 __u32 blk_size = osfs.os_bsize >> 10;
695 __u64 result = osfs.os_bavail;
697 while (blk_size >>= 1)
700 rc = seq_printf(m, LPU64"\n", result);
704 EXPORT_SYMBOL(lprocfs_kbytesavail_seq_show);
706 int lprocfs_filestotal_seq_show(struct seq_file *m, void *data)
708 struct obd_device *obd = data;
709 struct obd_statfs osfs;
710 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
711 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
714 rc = seq_printf(m, LPU64"\n", osfs.os_files);
717 EXPORT_SYMBOL(lprocfs_filestotal_seq_show);
719 int lprocfs_filesfree_seq_show(struct seq_file *m, void *data)
721 struct obd_device *obd = data;
722 struct obd_statfs osfs;
723 int rc = obd_statfs(NULL, obd->obd_self_export, &osfs,
724 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
727 rc = seq_printf(m, LPU64"\n", osfs.os_ffree);
730 EXPORT_SYMBOL(lprocfs_filesfree_seq_show);
732 int lprocfs_server_uuid_seq_show(struct seq_file *m, void *data)
734 struct obd_device *obd = data;
735 struct obd_import *imp;
736 char *imp_state_name = NULL;
739 LASSERT(obd != NULL);
740 LPROCFS_CLIMP_CHECK(obd);
741 imp = obd->u.cli.cl_import;
742 imp_state_name = ptlrpc_import_state_name(imp->imp_state);
743 rc = seq_printf(m, "%s\t%s%s\n", obd2cli_tgt(obd), imp_state_name,
744 imp->imp_deactive ? "\tDEACTIVATED" : "");
746 LPROCFS_CLIMP_EXIT(obd);
749 EXPORT_SYMBOL(lprocfs_server_uuid_seq_show);
751 int lprocfs_conn_uuid_seq_show(struct seq_file *m, void *data)
753 struct obd_device *obd = data;
754 struct ptlrpc_connection *conn;
757 LASSERT(obd != NULL);
759 LPROCFS_CLIMP_CHECK(obd);
760 conn = obd->u.cli.cl_import->imp_connection;
761 if (conn && obd->u.cli.cl_import)
762 rc = seq_printf(m, "%s\n", conn->c_remote_uuid.uuid);
764 rc = seq_printf(m, "%s\n", "<none>");
766 LPROCFS_CLIMP_EXIT(obd);
769 EXPORT_SYMBOL(lprocfs_conn_uuid_seq_show);
771 /** add up per-cpu counters */
772 void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx,
773 struct lprocfs_counter *cnt)
775 unsigned int num_entry;
776 struct lprocfs_counter *percpu_cntr;
778 unsigned long flags = 0;
780 memset(cnt, 0, sizeof(*cnt));
783 /* set count to 1 to avoid divide-by-zero errs in callers */
788 cnt->lc_min = LC_MIN_INIT;
790 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
792 for (i = 0; i < num_entry; i++) {
793 if (stats->ls_percpu[i] == NULL)
795 percpu_cntr = lprocfs_stats_counter_get(stats, i, idx);
797 cnt->lc_count += percpu_cntr->lc_count;
798 cnt->lc_sum += percpu_cntr->lc_sum;
799 if (percpu_cntr->lc_min < cnt->lc_min)
800 cnt->lc_min = percpu_cntr->lc_min;
801 if (percpu_cntr->lc_max > cnt->lc_max)
802 cnt->lc_max = percpu_cntr->lc_max;
803 cnt->lc_sumsquare += percpu_cntr->lc_sumsquare;
806 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
808 EXPORT_SYMBOL(lprocfs_stats_collect);
811 * Append a space separated list of current set flags to str.
813 #define flag2str(flag) \
815 if (imp->imp_##flag) { \
816 seq_printf(m, "%s" #flag, first ? "" : ", "); \
820 static void obd_import_flags2str(struct obd_import *imp, struct seq_file *m)
824 if (imp->imp_obd->obd_no_recov) {
825 seq_printf(m, "no_recov");
831 flag2str(replayable);
832 flag2str(delayed_recovery);
833 flag2str(no_lock_replay);
834 flag2str(vbr_failed);
836 flag2str(resend_replay);
837 flag2str(no_pinger_recover);
838 flag2str(need_mne_swab);
839 flag2str(connect_tried);
843 static const char *obd_connect_names[] = {
857 "join_file(obsolete)",
861 "remote_client_by_force",
870 "mds_mds_connection",
873 "alt_checksum_algorithm",
907 static void obd_connect_seq_flags2str(struct seq_file *m, __u64 flags, char *sep)
913 for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
915 seq_printf(m, "%s%s",
916 first ? "" : sep, obd_connect_names[i]);
920 if (flags & ~(mask - 1))
921 seq_printf(m, "%sunknown_"LPX64,
922 first ? "" : sep, flags & ~(mask - 1));
925 int obd_connect_flags2str(char *page, int count, __u64 flags, char *sep)
930 for (i = 0; obd_connect_names[i] != NULL; i++, mask <<= 1) {
932 ret += snprintf(page + ret, count - ret, "%s%s",
933 ret ? sep : "", obd_connect_names[i]);
935 if (flags & ~(mask - 1))
936 ret += snprintf(page + ret, count - ret,
938 ret ? sep : "", flags & ~(mask - 1));
941 EXPORT_SYMBOL(obd_connect_flags2str);
943 static void obd_connect_data_seqprint(struct seq_file *m,
944 struct obd_connect_data *ocd)
948 LASSERT(ocd != NULL);
949 flags = ocd->ocd_connect_flags;
951 seq_printf(m, " connect_data:\n"
954 ocd->ocd_connect_flags,
956 if (flags & OBD_CONNECT_VERSION)
957 seq_printf(m, " target_version: %u.%u.%u.%u\n",
958 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
959 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
960 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
961 OBD_OCD_VERSION_FIX(ocd->ocd_version));
962 if (flags & OBD_CONNECT_MDS)
963 seq_printf(m, " mdt_index: %d\n", ocd->ocd_group);
964 if (flags & OBD_CONNECT_GRANT)
965 seq_printf(m, " initial_grant: %d\n", ocd->ocd_grant);
966 if (flags & OBD_CONNECT_INDEX)
967 seq_printf(m, " target_index: %u\n", ocd->ocd_index);
968 if (flags & OBD_CONNECT_BRW_SIZE)
969 seq_printf(m, " max_brw_size: %d\n", ocd->ocd_brw_size);
970 if (flags & OBD_CONNECT_IBITS)
971 seq_printf(m, " ibits_known: "LPX64"\n",
972 ocd->ocd_ibits_known);
973 if (flags & OBD_CONNECT_GRANT_PARAM)
974 seq_printf(m, " grant_block_size: %d\n"
975 " grant_inode_size: %d\n"
976 " grant_extent_overhead: %d\n",
979 ocd->ocd_grant_extent);
980 if (flags & OBD_CONNECT_TRANSNO)
981 seq_printf(m, " first_transno: "LPX64"\n",
983 if (flags & OBD_CONNECT_CKSUM)
984 seq_printf(m, " cksum_types: %#x\n",
985 ocd->ocd_cksum_types);
986 if (flags & OBD_CONNECT_MAX_EASIZE)
987 seq_printf(m, " max_easize: %d\n", ocd->ocd_max_easize);
988 if (flags & OBD_CONNECT_MAXBYTES)
989 seq_printf(m, " max_object_bytes: "LPU64"\n",
993 int lprocfs_import_seq_show(struct seq_file *m, void *data)
995 struct lprocfs_counter ret;
996 struct lprocfs_counter_header *header;
997 struct obd_device *obd = (struct obd_device *)data;
998 struct obd_import *imp;
999 struct obd_import_conn *conn;
1000 struct obd_connect_data *ocd;
1005 LASSERT(obd != NULL);
1006 LPROCFS_CLIMP_CHECK(obd);
1007 imp = obd->u.cli.cl_import;
1008 ocd = &imp->imp_connect_data;
1010 seq_printf(m, "import:\n"
1014 " connect_flags: [ ",
1017 ptlrpc_import_state_name(imp->imp_state));
1018 obd_connect_seq_flags2str(m, imp->imp_connect_data.ocd_connect_flags,
1020 seq_printf(m, " ]\n");
1021 obd_connect_data_seqprint(m, ocd);
1022 seq_printf(m, " import_flags: [ ");
1023 obd_import_flags2str(imp, m);
1025 seq_printf(m, " ]\n"
1027 " failover_nids: [ ");
1028 spin_lock(&imp->imp_lock);
1030 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
1031 seq_printf(m, "%s%s", j ? ", " : "",
1032 libcfs_nid2str(conn->oic_conn->c_peer.nid));
1035 seq_printf(m, " ]\n"
1036 " current_connection: %s\n"
1037 " connection_attempts: %u\n"
1039 " in-progress_invalidations: %u\n",
1040 imp->imp_connection == NULL ? "<none>" :
1041 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1043 imp->imp_generation,
1044 atomic_read(&imp->imp_inval_count));
1045 spin_unlock(&imp->imp_lock);
1047 if (obd->obd_svc_stats == NULL)
1050 header = &obd->obd_svc_stats->ls_cnt_header[PTLRPC_REQWAIT_CNTR];
1051 lprocfs_stats_collect(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR, &ret);
1052 if (ret.lc_count != 0) {
1053 /* first argument to do_div MUST be __u64 */
1054 __u64 sum = ret.lc_sum;
1055 do_div(sum, ret.lc_count);
1059 seq_printf(m, " rpcs:\n"
1061 " unregistering: %u\n"
1063 " avg_waittime: "LPU64" %s\n",
1064 atomic_read(&imp->imp_inflight),
1065 atomic_read(&imp->imp_unregistering),
1066 atomic_read(&imp->imp_timeouts),
1067 ret.lc_sum, header->lc_units);
1070 for(j = 0; j < IMP_AT_MAX_PORTALS; j++) {
1071 if (imp->imp_at.iat_portal[j] == 0)
1073 k = max_t(unsigned int, k,
1074 at_get(&imp->imp_at.iat_service_estimate[j]));
1076 seq_printf(m, " service_estimates:\n"
1077 " services: %u sec\n"
1078 " network: %u sec\n",
1080 at_get(&imp->imp_at.iat_net_latency));
1082 seq_printf(m, " transactions:\n"
1083 " last_replay: "LPU64"\n"
1084 " peer_committed: "LPU64"\n"
1085 " last_checked: "LPU64"\n",
1086 imp->imp_last_replay_transno,
1087 imp->imp_peer_committed_transno,
1088 imp->imp_last_transno_checked);
1090 /* avg data rates */
1091 for (rw = 0; rw <= 1; rw++) {
1092 lprocfs_stats_collect(obd->obd_svc_stats,
1093 PTLRPC_LAST_CNTR + BRW_READ_BYTES + rw,
1095 if (ret.lc_sum > 0 && ret.lc_count > 0) {
1096 /* first argument to do_div MUST be __u64 */
1097 __u64 sum = ret.lc_sum;
1098 do_div(sum, ret.lc_count);
1100 seq_printf(m, " %s_data_averages:\n"
1101 " bytes_per_rpc: "LPU64"\n",
1102 rw ? "write" : "read",
1105 k = (int)ret.lc_sum;
1106 j = opcode_offset(OST_READ + rw) + EXTRA_MAX_OPCODES;
1107 header = &obd->obd_svc_stats->ls_cnt_header[j];
1108 lprocfs_stats_collect(obd->obd_svc_stats, j, &ret);
1109 if (ret.lc_sum > 0 && ret.lc_count != 0) {
1110 /* first argument to do_div MUST be __u64 */
1111 __u64 sum = ret.lc_sum;
1112 do_div(sum, ret.lc_count);
1114 seq_printf(m, " %s_per_rpc: "LPU64"\n",
1115 header->lc_units, ret.lc_sum);
1116 j = (int)ret.lc_sum;
1118 seq_printf(m, " MB_per_sec: %u.%.02u\n",
1119 k / j, (100 * k / j) % 100);
1124 LPROCFS_CLIMP_EXIT(obd);
1127 EXPORT_SYMBOL(lprocfs_import_seq_show);
1129 int lprocfs_state_seq_show(struct seq_file *m, void *data)
1131 struct obd_device *obd = (struct obd_device *)data;
1132 struct obd_import *imp;
1135 LASSERT(obd != NULL);
1136 LPROCFS_CLIMP_CHECK(obd);
1137 imp = obd->u.cli.cl_import;
1139 seq_printf(m, "current_state: %s\n",
1140 ptlrpc_import_state_name(imp->imp_state));
1141 seq_printf(m, "state_history:\n");
1142 k = imp->imp_state_hist_idx;
1143 for (j = 0; j < IMP_STATE_HIST_LEN; j++) {
1144 struct import_state_hist *ish =
1145 &imp->imp_state_hist[(k + j) % IMP_STATE_HIST_LEN];
1146 if (ish->ish_state == 0)
1148 seq_printf(m, " - [ "CFS_TIME_T", %s ]\n",
1150 ptlrpc_import_state_name(ish->ish_state));
1153 LPROCFS_CLIMP_EXIT(obd);
1156 EXPORT_SYMBOL(lprocfs_state_seq_show);
1158 int lprocfs_seq_at_hist_helper(struct seq_file *m, struct adaptive_timeout *at)
1161 for (i = 0; i < AT_BINS; i++)
1162 seq_printf(m, "%3u ", at->at_hist[i]);
1163 seq_printf(m, "\n");
1166 EXPORT_SYMBOL(lprocfs_seq_at_hist_helper);
1168 /* See also ptlrpc_lprocfs_timeouts_show_seq */
1169 int lprocfs_timeouts_seq_show(struct seq_file *m, void *data)
1171 struct obd_device *obd = (struct obd_device *)data;
1172 struct obd_import *imp;
1173 unsigned int cur, worst;
1178 LASSERT(obd != NULL);
1179 LPROCFS_CLIMP_CHECK(obd);
1180 imp = obd->u.cli.cl_import;
1182 now = cfs_time_current_sec();
1184 /* Some network health info for kicks */
1185 s2dhms(&ts, now - imp->imp_last_reply_time);
1186 seq_printf(m, "%-10s : %ld, "DHMS_FMT" ago\n",
1187 "last reply", imp->imp_last_reply_time, DHMS_VARS(&ts));
1189 cur = at_get(&imp->imp_at.iat_net_latency);
1190 worst = imp->imp_at.iat_net_latency.at_worst_ever;
1191 worstt = imp->imp_at.iat_net_latency.at_worst_time;
1192 s2dhms(&ts, now - worstt);
1193 seq_printf(m, "%-10s : cur %3u worst %3u (at %ld, "DHMS_FMT" ago) ",
1194 "network", cur, worst, worstt, DHMS_VARS(&ts));
1195 lprocfs_seq_at_hist_helper(m, &imp->imp_at.iat_net_latency);
1197 for(i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1198 if (imp->imp_at.iat_portal[i] == 0)
1200 cur = at_get(&imp->imp_at.iat_service_estimate[i]);
1201 worst = imp->imp_at.iat_service_estimate[i].at_worst_ever;
1202 worstt = imp->imp_at.iat_service_estimate[i].at_worst_time;
1203 s2dhms(&ts, now - worstt);
1204 seq_printf(m, "portal %-2d : cur %3u worst %3u (at %ld, "
1205 DHMS_FMT" ago) ", imp->imp_at.iat_portal[i],
1206 cur, worst, worstt, DHMS_VARS(&ts));
1207 lprocfs_seq_at_hist_helper(m, &imp->imp_at.iat_service_estimate[i]);
1210 LPROCFS_CLIMP_EXIT(obd);
1213 EXPORT_SYMBOL(lprocfs_timeouts_seq_show);
1215 int lprocfs_connect_flags_seq_show(struct seq_file *m, void *data)
1217 struct obd_device *obd = data;
1220 LPROCFS_CLIMP_CHECK(obd);
1221 flags = obd->u.cli.cl_import->imp_connect_data.ocd_connect_flags;
1222 seq_printf(m, "flags="LPX64"\n", flags);
1223 obd_connect_seq_flags2str(m, flags, "\n");
1224 seq_printf(m, "\n");
1225 LPROCFS_CLIMP_EXIT(obd);
1228 EXPORT_SYMBOL(lprocfs_connect_flags_seq_show);
1231 lprocfs_obd_setup(struct obd_device *obd)
1235 LASSERT(obd != NULL);
1236 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1237 LASSERT(obd->obd_type->typ_procroot != NULL);
1239 obd->obd_proc_entry = lprocfs_seq_register(obd->obd_name,
1240 obd->obd_type->typ_procroot,
1241 obd->obd_vars, obd);
1242 if (IS_ERR(obd->obd_proc_entry)) {
1243 rc = PTR_ERR(obd->obd_proc_entry);
1244 CERROR("error %d setting up lprocfs for %s\n",rc,obd->obd_name);
1245 obd->obd_proc_entry = NULL;
1249 EXPORT_SYMBOL(lprocfs_obd_setup);
1251 int lprocfs_obd_cleanup(struct obd_device *obd)
1255 if (obd->obd_proc_exports_entry) {
1256 /* Should be no exports left */
1257 lprocfs_remove(&obd->obd_proc_exports_entry);
1258 obd->obd_proc_exports_entry = NULL;
1260 if (obd->obd_proc_entry) {
1261 lprocfs_remove(&obd->obd_proc_entry);
1262 obd->obd_proc_entry = NULL;
1266 EXPORT_SYMBOL(lprocfs_obd_cleanup);
1268 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
1270 struct lprocfs_counter *cntr;
1271 unsigned int percpusize;
1273 unsigned long flags = 0;
1276 LASSERT(stats->ls_percpu[cpuid] == NULL);
1277 LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
1279 percpusize = lprocfs_stats_counter_size(stats);
1280 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
1281 if (stats->ls_percpu[cpuid] != NULL) {
1283 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
1284 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1285 spin_lock_irqsave(&stats->ls_lock, flags);
1287 spin_lock(&stats->ls_lock);
1288 if (stats->ls_biggest_alloc_num <= cpuid)
1289 stats->ls_biggest_alloc_num = cpuid + 1;
1290 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
1291 spin_unlock_irqrestore(&stats->ls_lock, flags);
1293 spin_unlock(&stats->ls_lock);
1296 /* initialize the ls_percpu[cpuid] non-zero counter */
1297 for (i = 0; i < stats->ls_num; ++i) {
1298 cntr = lprocfs_stats_counter_get(stats, cpuid, i);
1299 cntr->lc_min = LC_MIN_INIT;
1304 EXPORT_SYMBOL(lprocfs_stats_alloc_one);
1306 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
1307 enum lprocfs_stats_flags flags)
1309 struct lprocfs_stats *stats;
1310 unsigned int num_entry;
1311 unsigned int percpusize = 0;
1317 if (lprocfs_no_percpu_stats != 0)
1318 flags |= LPROCFS_STATS_FLAG_NOPERCPU;
1320 if (flags & LPROCFS_STATS_FLAG_NOPERCPU)
1323 num_entry = num_possible_cpus();
1325 /* alloc percpu pointers for all possible cpu slots */
1326 LIBCFS_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1330 stats->ls_num = num;
1331 stats->ls_flags = flags;
1332 spin_lock_init(&stats->ls_lock);
1334 /* alloc num of counter headers */
1335 LIBCFS_ALLOC(stats->ls_cnt_header,
1336 stats->ls_num * sizeof(struct lprocfs_counter_header));
1337 if (stats->ls_cnt_header == NULL)
1340 if ((flags & LPROCFS_STATS_FLAG_NOPERCPU) != 0) {
1341 /* contains only one set counters */
1342 percpusize = lprocfs_stats_counter_size(stats);
1343 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[0], percpusize);
1344 if (stats->ls_percpu[0] == NULL)
1346 stats->ls_biggest_alloc_num = 1;
1347 } else if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0) {
1348 /* alloc all percpu data, currently only obd_memory use this */
1349 for (i = 0; i < num_entry; ++i)
1350 if (lprocfs_stats_alloc_one(stats, i) < 0)
1357 lprocfs_free_stats(&stats);
1360 EXPORT_SYMBOL(lprocfs_alloc_stats);
1362 void lprocfs_free_stats(struct lprocfs_stats **statsh)
1364 struct lprocfs_stats *stats = *statsh;
1365 unsigned int num_entry;
1366 unsigned int percpusize;
1369 if (stats == NULL || stats->ls_num == 0)
1373 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
1376 num_entry = num_possible_cpus();
1378 percpusize = lprocfs_stats_counter_size(stats);
1379 for (i = 0; i < num_entry; i++)
1380 if (stats->ls_percpu[i] != NULL)
1381 LIBCFS_FREE(stats->ls_percpu[i], percpusize);
1382 if (stats->ls_cnt_header != NULL)
1383 LIBCFS_FREE(stats->ls_cnt_header, stats->ls_num *
1384 sizeof(struct lprocfs_counter_header));
1385 LIBCFS_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_entry]));
1387 EXPORT_SYMBOL(lprocfs_free_stats);
1389 void lprocfs_clear_stats(struct lprocfs_stats *stats)
1391 struct lprocfs_counter *percpu_cntr;
1394 unsigned int num_entry;
1395 unsigned long flags = 0;
1397 num_entry = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1399 for (i = 0; i < num_entry; i++) {
1400 if (stats->ls_percpu[i] == NULL)
1402 for (j = 0; j < stats->ls_num; j++) {
1403 percpu_cntr = lprocfs_stats_counter_get(stats, i, j);
1404 percpu_cntr->lc_count = 0;
1405 percpu_cntr->lc_min = LC_MIN_INIT;
1406 percpu_cntr->lc_max = 0;
1407 percpu_cntr->lc_sumsquare = 0;
1408 percpu_cntr->lc_sum = 0;
1409 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
1410 percpu_cntr->lc_sum_irq = 0;
1414 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1416 EXPORT_SYMBOL(lprocfs_clear_stats);
1418 static ssize_t lprocfs_stats_seq_write(struct file *file, const char *buf,
1419 size_t len, loff_t *off)
1421 struct seq_file *seq = file->private_data;
1422 struct lprocfs_stats *stats = seq->private;
1424 lprocfs_clear_stats(stats);
1429 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
1431 struct lprocfs_stats *stats = p->private;
1433 return (*pos < stats->ls_num) ? pos : NULL;
1436 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
1440 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
1444 return lprocfs_stats_seq_start(p, pos);
1447 /* seq file export of one lprocfs counter */
1448 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
1450 struct lprocfs_stats *stats = p->private;
1451 struct lprocfs_counter_header *hdr;
1452 struct lprocfs_counter ctr;
1453 int idx = *(loff_t *)v;
1459 do_gettimeofday(&now);
1460 rc = seq_printf(p, "%-25s %lu.%lu secs.usecs\n",
1461 "snapshot_time", now.tv_sec, now.tv_usec);
1466 hdr = &stats->ls_cnt_header[idx];
1467 lprocfs_stats_collect(stats, idx, &ctr);
1469 if (ctr.lc_count == 0)
1472 rc = seq_printf(p, "%-25s "LPD64" samples [%s]", hdr->lc_name,
1473 ctr.lc_count, hdr->lc_units);
1477 if ((hdr->lc_config & LPROCFS_CNTR_AVGMINMAX) && ctr.lc_count > 0) {
1478 rc = seq_printf(p, " "LPD64" "LPD64" "LPD64,
1479 ctr.lc_min, ctr.lc_max, ctr.lc_sum);
1482 if (hdr->lc_config & LPROCFS_CNTR_STDDEV)
1483 rc = seq_printf(p, " "LPD64, ctr.lc_sumsquare);
1487 rc = seq_printf(p, "\n");
1489 return (rc < 0) ? rc : 0;
1492 struct seq_operations lprocfs_stats_seq_sops = {
1493 .start = lprocfs_stats_seq_start,
1494 .stop = lprocfs_stats_seq_stop,
1495 .next = lprocfs_stats_seq_next,
1496 .show = lprocfs_stats_seq_show,
1499 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
1501 struct seq_file *seq;
1504 #ifndef HAVE_ONLY_PROCFS_SEQ
1505 if (LPROCFS_ENTRY_CHECK(PDE(inode)))
1508 rc = seq_open(file, &lprocfs_stats_seq_sops);
1511 seq = file->private_data;
1512 seq->private = PDE_DATA(inode);
1516 struct file_operations lprocfs_stats_seq_fops = {
1517 .owner = THIS_MODULE,
1518 .open = lprocfs_stats_seq_open,
1520 .write = lprocfs_stats_seq_write,
1521 .llseek = seq_lseek,
1522 .release = lprocfs_seq_release,
1525 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
1526 struct lprocfs_stats *stats)
1528 struct proc_dir_entry *entry;
1529 LASSERT(root != NULL);
1531 entry = proc_create_data(name, 0644, root,
1532 &lprocfs_stats_seq_fops, stats);
1537 EXPORT_SYMBOL(lprocfs_register_stats);
1539 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
1540 unsigned conf, const char *name, const char *units)
1542 struct lprocfs_counter_header *header;
1543 struct lprocfs_counter *percpu_cntr;
1544 unsigned long flags = 0;
1546 unsigned int num_cpu;
1548 LASSERT(stats != NULL);
1550 header = &stats->ls_cnt_header[index];
1551 LASSERTF(header != NULL, "Failed to allocate stats header:[%d]%s/%s\n",
1552 index, name, units);
1554 header->lc_config = conf;
1555 header->lc_name = name;
1556 header->lc_units = units;
1558 num_cpu = lprocfs_stats_lock(stats, LPROCFS_GET_NUM_CPU, &flags);
1559 for (i = 0; i < num_cpu; ++i) {
1560 if (stats->ls_percpu[i] == NULL)
1562 percpu_cntr = lprocfs_stats_counter_get(stats, i, index);
1563 percpu_cntr->lc_count = 0;
1564 percpu_cntr->lc_min = LC_MIN_INIT;
1565 percpu_cntr->lc_max = 0;
1566 percpu_cntr->lc_sumsquare = 0;
1567 percpu_cntr->lc_sum = 0;
1568 if ((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1569 percpu_cntr->lc_sum_irq = 0;
1571 lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags);
1573 EXPORT_SYMBOL(lprocfs_counter_init);
1575 /* Note that we only init md counters for ops whose offset is less
1576 * than NUM_MD_STATS. This is explained in a comment in the definition
1577 * of struct md_ops. */
1578 #define LPROCFS_MD_OP_INIT(base, stats, op) \
1580 unsigned int _idx = base + MD_COUNTER_OFFSET(op); \
1582 if (MD_COUNTER_OFFSET(op) < NUM_MD_STATS) { \
1583 LASSERT(_idx < stats->ls_num); \
1584 lprocfs_counter_init(stats, _idx, 0, #op, "reqs"); \
1588 void lprocfs_init_mps_stats(int num_private_stats, struct lprocfs_stats *stats)
1590 LPROCFS_MD_OP_INIT(num_private_stats, stats, getstatus);
1591 LPROCFS_MD_OP_INIT(num_private_stats, stats, null_inode);
1592 LPROCFS_MD_OP_INIT(num_private_stats, stats, find_cbdata);
1593 LPROCFS_MD_OP_INIT(num_private_stats, stats, close);
1594 LPROCFS_MD_OP_INIT(num_private_stats, stats, create);
1595 LPROCFS_MD_OP_INIT(num_private_stats, stats, done_writing);
1596 LPROCFS_MD_OP_INIT(num_private_stats, stats, enqueue);
1597 LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr);
1598 LPROCFS_MD_OP_INIT(num_private_stats, stats, getattr_name);
1599 LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_lock);
1600 LPROCFS_MD_OP_INIT(num_private_stats, stats, link);
1601 LPROCFS_MD_OP_INIT(num_private_stats, stats, rename);
1602 LPROCFS_MD_OP_INIT(num_private_stats, stats, setattr);
1603 LPROCFS_MD_OP_INIT(num_private_stats, stats, fsync);
1604 LPROCFS_MD_OP_INIT(num_private_stats, stats, read_page);
1605 LPROCFS_MD_OP_INIT(num_private_stats, stats, unlink);
1606 LPROCFS_MD_OP_INIT(num_private_stats, stats, setxattr);
1607 LPROCFS_MD_OP_INIT(num_private_stats, stats, getxattr);
1608 LPROCFS_MD_OP_INIT(num_private_stats, stats, init_ea_size);
1609 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_lustre_md);
1610 LPROCFS_MD_OP_INIT(num_private_stats, stats, free_lustre_md);
1611 LPROCFS_MD_OP_INIT(num_private_stats, stats, update_lsm_md);
1612 LPROCFS_MD_OP_INIT(num_private_stats, stats, merge_attr);
1613 LPROCFS_MD_OP_INIT(num_private_stats, stats, set_open_replay_data);
1614 LPROCFS_MD_OP_INIT(num_private_stats, stats, clear_open_replay_data);
1615 LPROCFS_MD_OP_INIT(num_private_stats, stats, set_lock_data);
1616 LPROCFS_MD_OP_INIT(num_private_stats, stats, lock_match);
1617 LPROCFS_MD_OP_INIT(num_private_stats, stats, cancel_unused);
1618 LPROCFS_MD_OP_INIT(num_private_stats, stats, renew_capa);
1619 LPROCFS_MD_OP_INIT(num_private_stats, stats, unpack_capa);
1620 LPROCFS_MD_OP_INIT(num_private_stats, stats, get_remote_perm);
1621 LPROCFS_MD_OP_INIT(num_private_stats, stats, intent_getattr_async);
1622 LPROCFS_MD_OP_INIT(num_private_stats, stats, revalidate_lock);
1624 EXPORT_SYMBOL(lprocfs_init_mps_stats);
1626 int lprocfs_alloc_md_stats(struct obd_device *obd,
1627 unsigned int num_private_stats)
1629 struct lprocfs_stats *stats;
1630 unsigned int num_stats;
1633 CLASSERT(offsetof(struct md_ops, MD_STATS_FIRST_OP) == 0);
1634 CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_FIRST_OP) == 0);
1635 CLASSERT(_MD_COUNTER_OFFSET(MD_STATS_LAST_OP) > 0);
1637 /* TODO Ensure that this function is only used where
1638 * appropriate by adding an assertion to the effect that
1639 * obd->obd_type->typ_md_ops is not NULL. We can't do this now
1640 * because mdt_procfs_init() uses this function to allocate
1641 * the stats backing /proc/fs/lustre/mdt/.../md_stats but the
1642 * mdt layer does not use the md_ops interface. This is
1643 * confusing and a waste of memory. See LU-2484.
1645 LASSERT(obd->obd_proc_entry != NULL);
1646 LASSERT(obd->obd_md_stats == NULL);
1647 LASSERT(obd->obd_md_cntr_base == 0);
1649 num_stats = NUM_MD_STATS + num_private_stats;
1650 stats = lprocfs_alloc_stats(num_stats, 0);
1654 lprocfs_init_mps_stats(num_private_stats, stats);
1656 for (i = num_private_stats; i < num_stats; i++) {
1657 if (stats->ls_cnt_header[i].lc_name == NULL) {
1658 CERROR("Missing md_stat initializer md_op "
1659 "operation at offset %d. Aborting.\n",
1660 i - num_private_stats);
1665 rc = lprocfs_register_stats(obd->obd_proc_entry, "md_stats", stats);
1667 lprocfs_free_stats(&stats);
1669 obd->obd_md_stats = stats;
1670 obd->obd_md_cntr_base = num_private_stats;
1675 EXPORT_SYMBOL(lprocfs_alloc_md_stats);
1677 void lprocfs_free_md_stats(struct obd_device *obd)
1679 struct lprocfs_stats *stats = obd->obd_md_stats;
1681 if (stats != NULL) {
1682 obd->obd_md_stats = NULL;
1683 obd->obd_md_cntr_base = 0;
1684 lprocfs_free_stats(&stats);
1687 EXPORT_SYMBOL(lprocfs_free_md_stats);
1689 void lprocfs_init_ldlm_stats(struct lprocfs_stats *ldlm_stats)
1691 lprocfs_counter_init(ldlm_stats,
1692 LDLM_ENQUEUE - LDLM_FIRST_OPC,
1693 0, "ldlm_enqueue", "reqs");
1694 lprocfs_counter_init(ldlm_stats,
1695 LDLM_CONVERT - LDLM_FIRST_OPC,
1696 0, "ldlm_convert", "reqs");
1697 lprocfs_counter_init(ldlm_stats,
1698 LDLM_CANCEL - LDLM_FIRST_OPC,
1699 0, "ldlm_cancel", "reqs");
1700 lprocfs_counter_init(ldlm_stats,
1701 LDLM_BL_CALLBACK - LDLM_FIRST_OPC,
1702 0, "ldlm_bl_callback", "reqs");
1703 lprocfs_counter_init(ldlm_stats,
1704 LDLM_CP_CALLBACK - LDLM_FIRST_OPC,
1705 0, "ldlm_cp_callback", "reqs");
1706 lprocfs_counter_init(ldlm_stats,
1707 LDLM_GL_CALLBACK - LDLM_FIRST_OPC,
1708 0, "ldlm_gl_callback", "reqs");
1710 EXPORT_SYMBOL(lprocfs_init_ldlm_stats);
1712 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
1713 struct lprocfs_counter_header *header,
1714 enum lprocfs_stats_flags flags,
1715 enum lprocfs_fields_flags field)
1719 if (lc == NULL || header == NULL)
1723 case LPROCFS_FIELDS_FLAGS_CONFIG:
1724 ret = header->lc_config;
1726 case LPROCFS_FIELDS_FLAGS_SUM:
1728 if ((flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
1729 ret += lc->lc_sum_irq;
1731 case LPROCFS_FIELDS_FLAGS_MIN:
1734 case LPROCFS_FIELDS_FLAGS_MAX:
1737 case LPROCFS_FIELDS_FLAGS_AVG:
1738 ret = (lc->lc_max - lc->lc_min) / 2;
1740 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
1741 ret = lc->lc_sumsquare;
1743 case LPROCFS_FIELDS_FLAGS_COUNT:
1751 EXPORT_SYMBOL(lprocfs_read_helper);
1753 int lprocfs_write_helper(const char *buffer, unsigned long count,
1756 return lprocfs_write_frac_helper(buffer, count, val, 1);
1758 EXPORT_SYMBOL(lprocfs_write_helper);
1760 int lprocfs_write_frac_helper(const char *buffer, unsigned long count,
1763 char kernbuf[20], *end, *pbuf;
1765 if (count > (sizeof(kernbuf) - 1))
1768 if (copy_from_user(kernbuf, buffer, count))
1771 kernbuf[count] = '\0';
1778 *val = (int)simple_strtoul(pbuf, &end, 10) * mult;
1782 if (end != NULL && *end == '.') {
1783 int temp_val, pow = 1;
1787 if (strlen(pbuf) > 5)
1788 pbuf[5] = '\0'; /*only allow 5bits fractional*/
1790 temp_val = (int)simple_strtoul(pbuf, &end, 10) * mult;
1793 for (i = 0; i < (end - pbuf); i++)
1796 *val += temp_val / pow;
1801 EXPORT_SYMBOL(lprocfs_write_frac_helper);
1803 int lprocfs_read_frac_helper(char *buffer, unsigned long count, long val,
1806 long decimal_val, frac_val;
1812 decimal_val = val / mult;
1813 prtn = snprintf(buffer, count, "%ld", decimal_val);
1814 frac_val = val % mult;
1816 if (prtn < (count - 4) && frac_val > 0) {
1818 int i, temp_mult = 1, frac_bits = 0;
1820 temp_frac = frac_val * 10;
1821 buffer[prtn++] = '.';
1822 while (frac_bits < 2 && (temp_frac / mult) < 1 ) {
1823 /* only reserved 2 bits fraction */
1824 buffer[prtn++] ='0';
1829 * Need to think these cases :
1830 * 1. #echo x.00 > /proc/xxx output result : x
1831 * 2. #echo x.0x > /proc/xxx output result : x.0x
1832 * 3. #echo x.x0 > /proc/xxx output result : x.x
1833 * 4. #echo x.xx > /proc/xxx output result : x.xx
1834 * Only reserved 2 bits fraction.
1836 for (i = 0; i < (5 - prtn); i++)
1839 frac_bits = min((int)count - prtn, 3 - frac_bits);
1840 prtn += snprintf(buffer + prtn, frac_bits, "%ld",
1841 frac_val * temp_mult / mult);
1844 while(buffer[prtn] < '1' || buffer[prtn] > '9') {
1846 if (buffer[prtn] == '.') {
1853 buffer[prtn++] ='\n';
1856 EXPORT_SYMBOL(lprocfs_read_frac_helper);
1858 int lprocfs_seq_read_frac_helper(struct seq_file *m, long val, int mult)
1860 long decimal_val, frac_val;
1862 decimal_val = val / mult;
1863 seq_printf(m, "%ld", decimal_val);
1864 frac_val = val % mult;
1871 /* Three cases: x0, xx, 0x */
1872 if ((frac_val % 10) != 0)
1873 seq_printf(m, ".%ld", frac_val);
1875 seq_printf(m, ".%ld", frac_val / 10);
1878 seq_printf(m, "\n");
1881 EXPORT_SYMBOL(lprocfs_seq_read_frac_helper);
1883 int lprocfs_write_u64_helper(const char *buffer, unsigned long count,__u64 *val)
1885 return lprocfs_write_frac_u64_helper(buffer, count, val, 1);
1887 EXPORT_SYMBOL(lprocfs_write_u64_helper);
1889 int lprocfs_write_frac_u64_helper(const char *buffer, unsigned long count,
1890 __u64 *val, int mult)
1892 char kernbuf[22], *end, *pbuf;
1893 __u64 whole, frac = 0, units;
1894 unsigned frac_d = 1;
1896 if (count > (sizeof(kernbuf) - 1))
1899 if (copy_from_user(kernbuf, buffer, count))
1902 kernbuf[count] = '\0';
1909 whole = simple_strtoull(pbuf, &end, 10);
1913 if (end != NULL && *end == '.') {
1917 /* need to limit frac_d to a __u32 */
1918 if (strlen(pbuf) > 10)
1921 frac = simple_strtoull(pbuf, &end, 10);
1922 /* count decimal places */
1923 for (i = 0; i < (end - pbuf); i++)
1942 /* Specified units override the multiplier */
1944 mult = mult < 0 ? -units : units;
1947 do_div(frac, frac_d);
1948 *val = whole * mult + frac;
1951 EXPORT_SYMBOL(lprocfs_write_frac_u64_helper);
1953 static char *lprocfs_strnstr(const char *s1, const char *s2, size_t len)
1962 if (!memcmp(s1, s2, l2))
1970 * Find the string \a name in the input \a buffer, and return a pointer to the
1971 * value immediately following \a name, reducing \a count appropriately.
1972 * If \a name is not found the original \a buffer is returned.
1974 char *lprocfs_find_named_value(const char *buffer, const char *name,
1978 size_t buflen = *count;
1980 /* there is no strnstr() in rhel5 and ubuntu kernels */
1981 val = lprocfs_strnstr(buffer, name, buflen);
1983 return (char *)buffer;
1985 val += strlen(name); /* skip prefix */
1986 while (val < buffer + buflen && isspace(*val)) /* skip separator */
1990 while (val < buffer + buflen && isalnum(*val)) {
1995 return val - *count;
1997 EXPORT_SYMBOL(lprocfs_find_named_value);
1999 int lprocfs_seq_create(struct proc_dir_entry *parent,
2002 const struct file_operations *seq_fops,
2005 struct proc_dir_entry *entry;
2008 /* Disallow secretly (un)writable entries. */
2009 LASSERT((seq_fops->write == NULL) == ((mode & 0222) == 0));
2011 entry = proc_create_data(name, mode, parent, seq_fops, data);
2018 EXPORT_SYMBOL(lprocfs_seq_create);
2020 int lprocfs_obd_seq_create(struct obd_device *dev,
2023 const struct file_operations *seq_fops,
2026 return (lprocfs_seq_create(dev->obd_proc_entry, name,
2027 mode, seq_fops, data));
2029 EXPORT_SYMBOL(lprocfs_obd_seq_create);
2031 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value)
2033 if (value >= OBD_HIST_MAX)
2034 value = OBD_HIST_MAX - 1;
2036 spin_lock(&oh->oh_lock);
2037 oh->oh_buckets[value]++;
2038 spin_unlock(&oh->oh_lock);
2040 EXPORT_SYMBOL(lprocfs_oh_tally);
2042 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value)
2044 unsigned int val = 0;
2046 if (likely(value != 0))
2047 val = min(fls(value - 1), OBD_HIST_MAX);
2049 lprocfs_oh_tally(oh, val);
2051 EXPORT_SYMBOL(lprocfs_oh_tally_log2);
2053 unsigned long lprocfs_oh_sum(struct obd_histogram *oh)
2055 unsigned long ret = 0;
2058 for (i = 0; i < OBD_HIST_MAX; i++)
2059 ret += oh->oh_buckets[i];
2062 EXPORT_SYMBOL(lprocfs_oh_sum);
2064 void lprocfs_oh_clear(struct obd_histogram *oh)
2066 spin_lock(&oh->oh_lock);
2067 memset(oh->oh_buckets, 0, sizeof(oh->oh_buckets));
2068 spin_unlock(&oh->oh_lock);
2070 EXPORT_SYMBOL(lprocfs_oh_clear);
2072 int lprocfs_obd_rd_max_pages_per_rpc(char *page, char **start, off_t off,
2073 int count, int *eof, void *data)
2075 struct obd_device *dev = data;
2076 struct client_obd *cli = &dev->u.cli;
2079 client_obd_list_lock(&cli->cl_loi_list_lock);
2080 rc = snprintf(page, count, "%d\n", cli->cl_max_pages_per_rpc);
2081 client_obd_list_unlock(&cli->cl_loi_list_lock);
2084 EXPORT_SYMBOL(lprocfs_obd_rd_max_pages_per_rpc);
2086 int lprocfs_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *data)
2088 struct obd_device *dev = data;
2089 struct client_obd *cli = &dev->u.cli;
2092 client_obd_list_lock(&cli->cl_loi_list_lock);
2093 rc = seq_printf(m, "%d\n", cli->cl_max_pages_per_rpc);
2094 client_obd_list_unlock(&cli->cl_loi_list_lock);
2097 EXPORT_SYMBOL(lprocfs_obd_max_pages_per_rpc_seq_show);
2099 int lprocfs_wr_root_squash(const char __user *buffer, unsigned long count,
2100 struct root_squash_info *squash, char *name)
2103 char kernbuf[64], *tmp, *errmsg;
2104 unsigned long uid, gid;
2107 if (count >= sizeof(kernbuf)) {
2108 errmsg = "string too long";
2109 GOTO(failed_noprint, rc = -EINVAL);
2111 if (copy_from_user(kernbuf, buffer, count)) {
2112 errmsg = "bad address";
2113 GOTO(failed_noprint, rc = -EFAULT);
2115 kernbuf[count] = '\0';
2117 /* look for uid gid separator */
2118 tmp = strchr(kernbuf, ':');
2120 errmsg = "needs uid:gid format";
2121 GOTO(failed, rc = -EINVAL);
2127 if (kstrtoul(kernbuf, 0, &uid) != 0) {
2129 GOTO(failed, rc = -EINVAL);
2133 if (kstrtoul(tmp, 0, &gid) != 0) {
2135 GOTO(failed, rc = -EINVAL);
2138 squash->rsi_uid = uid;
2139 squash->rsi_gid = gid;
2141 LCONSOLE_INFO("%s: root_squash is set to %u:%u\n",
2142 name, squash->rsi_uid, squash->rsi_gid);
2150 CWARN("%s: failed to set root_squash to \"%s\", %s, rc = %d\n",
2151 name, kernbuf, errmsg, rc);
2154 CWARN("%s: failed to set root_squash due to %s, rc = %d\n",
2158 EXPORT_SYMBOL(lprocfs_wr_root_squash);
2161 int lprocfs_wr_nosquash_nids(const char __user *buffer, unsigned long count,
2162 struct root_squash_info *squash, char *name)
2165 char *kernbuf = NULL;
2167 struct list_head tmp;
2171 errmsg = "string too long";
2172 GOTO(failed, rc = -EINVAL);
2175 OBD_ALLOC(kernbuf, count + 1);
2176 if (kernbuf == NULL) {
2177 errmsg = "no memory";
2178 GOTO(failed, rc = -ENOMEM);
2180 if (copy_from_user(kernbuf, buffer, count)) {
2181 errmsg = "bad address";
2182 GOTO(failed, rc = -EFAULT);
2184 kernbuf[count] = '\0';
2186 if (count > 0 && kernbuf[count - 1] == '\n')
2187 kernbuf[count - 1] = '\0';
2189 if (strcmp(kernbuf, "NONE") == 0 || strcmp(kernbuf, "clear") == 0) {
2190 /* empty string is special case */
2191 down_write(&squash->rsi_sem);
2192 if (!list_empty(&squash->rsi_nosquash_nids))
2193 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2194 up_write(&squash->rsi_sem);
2195 LCONSOLE_INFO("%s: nosquash_nids is cleared\n", name);
2196 OBD_FREE(kernbuf, count + 1);
2200 INIT_LIST_HEAD(&tmp);
2201 if (cfs_parse_nidlist(kernbuf, count, &tmp) <= 0) {
2202 errmsg = "can't parse";
2203 GOTO(failed, rc = -EINVAL);
2205 LCONSOLE_INFO("%s: nosquash_nids set to %s\n",
2207 OBD_FREE(kernbuf, count + 1);
2210 down_write(&squash->rsi_sem);
2211 if (!list_empty(&squash->rsi_nosquash_nids))
2212 cfs_free_nidlist(&squash->rsi_nosquash_nids);
2213 list_splice(&tmp, &squash->rsi_nosquash_nids);
2214 up_write(&squash->rsi_sem);
2220 CWARN("%s: failed to set nosquash_nids to \"%s\", %s rc = %d\n",
2221 name, kernbuf, errmsg, rc);
2222 OBD_FREE(kernbuf, count + 1);
2224 CWARN("%s: failed to set nosquash_nids due to %s rc = %d\n",
2229 EXPORT_SYMBOL(lprocfs_wr_nosquash_nids);