1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5 * Author: Hariharan Thantry <thantry@users.sourceforge.net>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define DEBUG_SUBSYSTEM S_CLASS
27 # include <linux/config.h>
28 # include <linux/module.h>
29 # include <linux/version.h>
30 # include <linux/slab.h>
31 # include <linux/types.h>
32 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
33 # include <asm/statfs.h>
35 # include <linux/seq_file.h>
36 #else /* __KERNEL__ */
37 # include <liblustre.h>
40 #include <linux/obd_class.h>
41 #include <linux/lprocfs_status.h>
42 #include <linux/lustre_fsfilt.h>
46 struct proc_dir_entry *lprocfs_srch(struct proc_dir_entry *head,
49 struct proc_dir_entry *temp;
55 while (temp != NULL) {
56 if (strcmp(temp->name, name) == 0)
64 /* lprocfs API calls */
66 int lprocfs_add_vars(struct proc_dir_entry *root, struct lprocfs_vars *list,
69 if (root == NULL || list == NULL)
72 while (list->name != NULL) {
73 struct proc_dir_entry *cur_root, *proc;
74 char *pathcopy, *cur, *next, pathbuf[64];
75 int pathsize = strlen(list->name) + 1;
80 /* need copy of path for strsep */
81 if (strlen(list->name) > sizeof(pathbuf) - 1) {
82 OBD_ALLOC(pathcopy, pathsize);
90 strcpy(pathcopy, list->name);
92 while (cur_root != NULL && (cur = strsep(&next, "/"))) {
93 if (*cur =='\0') /* skip double/trailing "/" */
96 proc = lprocfs_srch(cur_root, cur);
97 CDEBUG(D_OTHER, "cur_root=%s, cur=%s, next=%s, (%s)\n",
98 cur_root->name, cur, next,
99 (proc ? "exists" : "new"));
101 cur_root = (proc ? proc :
102 proc_mkdir(cur, cur_root));
103 } else if (proc == NULL) {
105 if (list->write_fptr)
107 proc = create_proc_entry(cur, mode, cur_root);
111 if (pathcopy != pathbuf)
112 OBD_FREE(pathcopy, pathsize);
114 if (cur_root == NULL || proc == NULL) {
115 CERROR("LprocFS: No memory to create /proc entry %s",
120 proc->read_proc = list->read_fptr;
121 proc->write_proc = list->write_fptr;
122 proc->data = (list->data ? list->data : data);
128 void lprocfs_remove(struct proc_dir_entry *root)
130 struct proc_dir_entry *temp = root;
131 struct proc_dir_entry *rm_entry;
132 struct proc_dir_entry *parent;
134 LASSERT(root != NULL);
135 parent = root->parent;
136 LASSERT(parent != NULL);
139 while (temp->subdir != NULL)
144 remove_proc_entry(rm_entry->name, rm_entry->parent);
150 struct proc_dir_entry *lprocfs_register(const char *name,
151 struct proc_dir_entry *parent,
152 struct lprocfs_vars *list, void *data)
154 struct proc_dir_entry *newchild;
156 newchild = lprocfs_srch(parent, name);
157 if (newchild != NULL) {
158 CERROR(" Lproc: Attempting to register %s more than once \n",
160 return ERR_PTR(-EALREADY);
163 newchild = proc_mkdir(name, parent);
164 if (newchild != NULL && list != NULL) {
165 int rc = lprocfs_add_vars(newchild, list, data);
167 lprocfs_remove(newchild);
174 /* Generic callbacks */
176 int lprocfs_rd_u64(char *page, char **start, off_t off,
177 int count, int *eof, void *data)
179 LASSERT(data != NULL);
181 return snprintf(page, count, LPU64"\n", *(__u64 *)data);
184 int lprocfs_rd_uuid(char *page, char **start, off_t off, int count,
185 int *eof, void *data)
187 struct obd_device *dev = (struct obd_device*)data;
189 LASSERT(dev != NULL);
191 return snprintf(page, count, "%s\n", dev->obd_uuid.uuid);
194 int lprocfs_rd_name(char *page, char **start, off_t off, int count,
195 int *eof, void* data)
197 struct obd_device *dev = (struct obd_device *)data;
199 LASSERT(dev != NULL);
200 LASSERT(dev->obd_name != NULL);
202 return snprintf(page, count, "%s\n", dev->obd_name);
205 int lprocfs_rd_fstype(char *page, char **start, off_t off, int count, int *eof,
208 struct obd_device *obd = (struct obd_device *)data;
210 LASSERT(obd != NULL);
211 LASSERT(obd->obd_fsops != NULL);
212 LASSERT(obd->obd_fsops->fs_type != NULL);
213 return snprintf(page, count, "%s\n", obd->obd_fsops->fs_type);
216 int lprocfs_rd_blksize(char *page, char **start, off_t off, int count,
217 int *eof, void *data)
219 struct obd_statfs osfs;
220 int rc = obd_statfs(data, &osfs, jiffies - HZ);
223 rc = snprintf(page, count, "%u\n", osfs.os_bsize);
228 int lprocfs_rd_kbytestotal(char *page, char **start, off_t off, int count,
229 int *eof, void *data)
231 struct obd_statfs osfs;
232 int rc = obd_statfs(data, &osfs, jiffies - HZ);
234 __u32 blk_size = osfs.os_bsize >> 10;
235 __u64 result = osfs.os_blocks;
237 while (blk_size >>= 1)
241 rc = snprintf(page, count, LPU64"\n", result);
246 int lprocfs_rd_kbytesfree(char *page, char **start, off_t off, int count,
247 int *eof, void *data)
249 struct obd_statfs osfs;
250 int rc = obd_statfs(data, &osfs, jiffies - HZ);
252 __u32 blk_size = osfs.os_bsize >> 10;
253 __u64 result = osfs.os_bfree;
255 while (blk_size >>= 1)
259 rc = snprintf(page, count, LPU64"\n", result);
264 int lprocfs_rd_filestotal(char *page, char **start, off_t off, int count,
265 int *eof, void *data)
267 struct obd_statfs osfs;
268 int rc = obd_statfs(data, &osfs, jiffies - HZ);
271 rc = snprintf(page, count, LPU64"\n", osfs.os_files);
277 int lprocfs_rd_filesfree(char *page, char **start, off_t off, int count,
278 int *eof, void *data)
280 struct obd_statfs osfs;
281 int rc = obd_statfs(data, &osfs, jiffies - HZ);
284 rc = snprintf(page, count, LPU64"\n", osfs.os_ffree);
289 int lprocfs_rd_filegroups(char *page, char **start, off_t off, int count,
290 int *eof, void *data)
293 return snprintf(page, count, "unimplemented\n");
296 int lprocfs_rd_server_uuid(char *page, char **start, off_t off, int count,
297 int *eof, void *data)
299 struct obd_device *obd = (struct obd_device *)data;
300 struct client_obd *cli;
302 LASSERT(obd != NULL);
305 return snprintf(page, count, "%s\n",
306 cli->cl_import->imp_target_uuid.uuid);
309 int lprocfs_rd_conn_uuid(char *page, char **start, off_t off, int count,
310 int *eof, void *data)
312 struct obd_device *obd = (struct obd_device*)data;
313 struct ptlrpc_connection *conn;
315 LASSERT(obd != NULL);
316 conn = obd->u.cli.cl_import->imp_connection;
317 LASSERT(conn != NULL);
319 return snprintf(page, count, "%s\n", conn->c_remote_uuid.uuid);
322 int lprocfs_rd_numrefs(char *page, char **start, off_t off, int count,
323 int *eof, void *data)
325 struct obd_type *class = (struct obd_type*) data;
327 LASSERT(class != NULL);
329 return snprintf(page, count, "%d\n", class->typ_refcnt);
332 int lprocfs_obd_attach(struct obd_device *dev, struct lprocfs_vars *list)
336 LASSERT(dev != NULL);
337 LASSERT(dev->obd_type != NULL);
338 LASSERT(dev->obd_type->typ_procroot != NULL);
340 dev->obd_proc_entry = lprocfs_register(dev->obd_name,
341 dev->obd_type->typ_procroot,
343 if (IS_ERR(dev->obd_proc_entry)) {
344 rc = PTR_ERR(dev->obd_proc_entry);
345 dev->obd_proc_entry = NULL;
350 int lprocfs_obd_detach(struct obd_device *dev)
352 if (dev && dev->obd_proc_entry) {
353 lprocfs_remove(dev->obd_proc_entry);
354 dev->obd_proc_entry = NULL;
359 struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num)
361 struct lprocfs_stats *stats;
362 struct lprocfs_percpu *percpu;
363 unsigned int percpusize;
369 OBD_ALLOC(stats, offsetof(typeof(*stats), ls_percpu[num_online_cpus()]));
373 percpusize = L1_CACHE_ALIGN(offsetof(typeof(*percpu), lp_cntr[num]));
374 stats->ls_percpu_size = num_online_cpus() * percpusize;
375 OBD_ALLOC(stats->ls_percpu[0], stats->ls_percpu_size);
376 if (stats->ls_percpu[0] == NULL) {
377 OBD_FREE(stats, offsetof(typeof(*stats),
378 ls_percpu[num_online_cpus()]));
383 for (i = 1; i < num_online_cpus(); i++)
384 stats->ls_percpu[i] = (void *)(stats->ls_percpu[i - 1]) +
390 void lprocfs_free_stats(struct lprocfs_stats *stats)
392 if (stats->ls_num == 0)
395 OBD_FREE(stats->ls_percpu[0], stats->ls_percpu_size);
396 OBD_FREE(stats, offsetof(typeof(*stats), ls_percpu[num_online_cpus()]));
399 /* Reset counter under lock */
400 int lprocfs_counter_write(struct file *file, const char *buffer,
401 unsigned long count, void *data)
407 static void *lprocfs_stats_seq_start(struct seq_file *p, loff_t *pos)
409 struct lprocfs_stats *stats = p->private;
410 /* return 1st cpu location */
411 return (*pos >= stats->ls_num) ? NULL :
412 &(stats->ls_percpu[0]->lp_cntr[*pos]);
415 static void lprocfs_stats_seq_stop(struct seq_file *p, void *v)
419 static void *lprocfs_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
421 struct lprocfs_stats *stats = p->private;
423 return (*pos >= stats->ls_num) ? NULL :
424 &(stats->ls_percpu[0]->lp_cntr[*pos]);
427 /* seq file export of one lprocfs counter */
428 static int lprocfs_stats_seq_show(struct seq_file *p, void *v)
430 struct lprocfs_stats *stats = p->private;
431 struct lprocfs_counter *cntr = v;
432 struct lprocfs_counter t, ret = { .lc_min = ~(__u64)0 };
435 if (cntr == &(stats->ls_percpu[0])->lp_cntr[0]) {
437 do_gettimeofday(&now);
438 rc = seq_printf(p, "%-25s %lu.%lu secs.usecs\n",
439 "snapshot_time", now.tv_sec, now.tv_usec);
443 idx = cntr - &(stats->ls_percpu[0])->lp_cntr[0];
445 for (i = 0; i < num_online_cpus(); i++) {
446 struct lprocfs_counter *percpu_cntr =
447 &(stats->ls_percpu[i])->lp_cntr[idx];
451 centry = atomic_read(&percpu_cntr->lc_cntl.la_entry);
452 t.lc_count = percpu_cntr->lc_count;
453 t.lc_sum = percpu_cntr->lc_sum;
454 t.lc_min = percpu_cntr->lc_min;
455 t.lc_max = percpu_cntr->lc_max;
456 t.lc_sumsquare = percpu_cntr->lc_sumsquare;
457 } while (centry != atomic_read(&percpu_cntr->lc_cntl.la_entry) &&
458 centry != atomic_read(&percpu_cntr->lc_cntl.la_exit));
459 ret.lc_count += t.lc_count;
460 ret.lc_sum += t.lc_sum;
461 if (t.lc_min < ret.lc_min)
462 ret.lc_min = t.lc_min;
463 if (t.lc_max > ret.lc_max)
464 ret.lc_max = t.lc_max;
465 ret.lc_sumsquare += t.lc_sumsquare;
468 rc = seq_printf(p, "%-25s "LPU64" samples [%s]", cntr->lc_name,
469 ret.lc_count, cntr->lc_units);
473 if ((cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) && (ret.lc_count > 0)) {
474 rc = seq_printf(p, " "LPU64" "LPU64" "LPU64,
475 ret.lc_min, ret.lc_max, ret.lc_sum);
478 if (cntr->lc_config & LPROCFS_CNTR_STDDEV)
479 rc = seq_printf(p, " "LPU64, ret.lc_sumsquare);
483 rc = seq_printf(p, "\n");
485 return (rc < 0) ? rc : 0;
488 struct seq_operations lprocfs_stats_seq_sops = {
489 start: lprocfs_stats_seq_start,
490 stop: lprocfs_stats_seq_stop,
491 next: lprocfs_stats_seq_next,
492 show: lprocfs_stats_seq_show,
495 static int lprocfs_stats_seq_open(struct inode *inode, struct file *file)
497 struct proc_dir_entry *dp = inode->u.generic_ip;
498 struct seq_file *seq;
501 rc = seq_open(file, &lprocfs_stats_seq_sops);
504 seq = file->private_data;
505 seq->private = dp->data;
509 struct file_operations lprocfs_stats_seq_fops = {
510 open: lprocfs_stats_seq_open,
513 release: seq_release,
516 int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
517 struct lprocfs_stats *stats)
519 struct proc_dir_entry *entry;
520 LASSERT(root != NULL);
522 entry = create_proc_entry(name, 0444, root);
525 entry->proc_fops = &lprocfs_stats_seq_fops;
526 entry->data = (void *)stats;
527 entry->write_proc = lprocfs_counter_write;
531 void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
532 unsigned conf, const char *name, const char *units)
534 struct lprocfs_counter *c;
537 LASSERT(stats != NULL);
538 for (i = 0; i < num_online_cpus(); i++) {
539 c = &(stats->ls_percpu[i]->lp_cntr[index]);
541 c->lc_min = ~(__u64)0;
546 EXPORT_SYMBOL(lprocfs_counter_init);
548 #define LPROCFS_OBD_OP_INIT(base, stats, op) \
550 unsigned int coffset = base + OBD_COUNTER_OFFSET(op); \
551 LASSERT(coffset < stats->ls_num); \
552 lprocfs_counter_init(stats, coffset, 0, #op, "reqs"); \
555 int lprocfs_alloc_obd_stats(struct obd_device *obd, unsigned num_private_stats)
557 struct lprocfs_stats *stats;
558 unsigned int num_stats;
561 LASSERT(obd->obd_stats == NULL);
562 LASSERT(obd->obd_proc_entry != NULL);
563 LASSERT(obd->obd_cntr_base == 0);
565 num_stats = 1 + OBD_COUNTER_OFFSET(unpin) +
567 stats = lprocfs_alloc_stats(num_stats);
571 LPROCFS_OBD_OP_INIT(num_private_stats, stats, iocontrol);
572 LPROCFS_OBD_OP_INIT(num_private_stats, stats, get_info);
573 LPROCFS_OBD_OP_INIT(num_private_stats, stats, set_info);
574 LPROCFS_OBD_OP_INIT(num_private_stats, stats, attach);
575 LPROCFS_OBD_OP_INIT(num_private_stats, stats, detach);
576 LPROCFS_OBD_OP_INIT(num_private_stats, stats, setup);
577 LPROCFS_OBD_OP_INIT(num_private_stats, stats, cleanup);
578 LPROCFS_OBD_OP_INIT(num_private_stats, stats, connect);
579 LPROCFS_OBD_OP_INIT(num_private_stats, stats, disconnect);
580 LPROCFS_OBD_OP_INIT(num_private_stats, stats, statfs);
581 LPROCFS_OBD_OP_INIT(num_private_stats, stats, syncfs);
582 LPROCFS_OBD_OP_INIT(num_private_stats, stats, packmd);
583 LPROCFS_OBD_OP_INIT(num_private_stats, stats, unpackmd);
584 LPROCFS_OBD_OP_INIT(num_private_stats, stats, preallocate);
585 LPROCFS_OBD_OP_INIT(num_private_stats, stats, create);
586 LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy);
587 LPROCFS_OBD_OP_INIT(num_private_stats, stats, setattr);
588 LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr);
589 LPROCFS_OBD_OP_INIT(num_private_stats, stats, getattr_async);
590 LPROCFS_OBD_OP_INIT(num_private_stats, stats, open);
591 LPROCFS_OBD_OP_INIT(num_private_stats, stats, close);
592 LPROCFS_OBD_OP_INIT(num_private_stats, stats, brw);
593 LPROCFS_OBD_OP_INIT(num_private_stats, stats, brw_async);
594 LPROCFS_OBD_OP_INIT(num_private_stats, stats, punch);
595 LPROCFS_OBD_OP_INIT(num_private_stats, stats, sync);
596 LPROCFS_OBD_OP_INIT(num_private_stats, stats, migrate);
597 LPROCFS_OBD_OP_INIT(num_private_stats, stats, copy);
598 LPROCFS_OBD_OP_INIT(num_private_stats, stats, iterate);
599 LPROCFS_OBD_OP_INIT(num_private_stats, stats, preprw);
600 LPROCFS_OBD_OP_INIT(num_private_stats, stats, commitrw);
601 LPROCFS_OBD_OP_INIT(num_private_stats, stats, enqueue);
602 LPROCFS_OBD_OP_INIT(num_private_stats, stats, match);
603 LPROCFS_OBD_OP_INIT(num_private_stats, stats, cancel);
604 LPROCFS_OBD_OP_INIT(num_private_stats, stats, cancel_unused);
605 LPROCFS_OBD_OP_INIT(num_private_stats, stats, log_add);
606 LPROCFS_OBD_OP_INIT(num_private_stats, stats, log_cancel);
607 LPROCFS_OBD_OP_INIT(num_private_stats, stats, san_preprw);
608 LPROCFS_OBD_OP_INIT(num_private_stats, stats, mark_page_dirty);
609 LPROCFS_OBD_OP_INIT(num_private_stats, stats, clear_dirty_pages);
610 LPROCFS_OBD_OP_INIT(num_private_stats, stats, last_dirty_offset);
611 LPROCFS_OBD_OP_INIT(num_private_stats, stats, destroy_export);
612 LPROCFS_OBD_OP_INIT(num_private_stats, stats, pin);
613 LPROCFS_OBD_OP_INIT(num_private_stats, stats, unpin);
615 for (i = num_private_stats; i < num_stats; i++) {
616 /* If this LBUGs, it is likely that an obd
617 * operation was added to struct obd_ops in
618 * <linux/obd.h>, and that the corresponding line item
619 * LPROCFS_OBD_OP_INIT(.., .., opname)
620 * is missing from the list above. */
621 if (stats->ls_percpu[0]->lp_cntr[i].lc_name == NULL) {
622 CERROR("Missing obd_stat initializer obd_op "
623 "operation at offset %d. Aborting.\n",
624 i - num_private_stats);
628 rc = lprocfs_register_stats(obd->obd_proc_entry, "stats", stats);
630 lprocfs_free_stats(stats);
632 obd->obd_stats = stats;
633 obd->obd_cntr_base = num_private_stats;
638 void lprocfs_free_obd_stats(struct obd_device *obd)
640 struct lprocfs_stats *stats = obd->obd_stats;
643 obd->obd_stats = NULL;
644 lprocfs_free_stats(stats);
650 EXPORT_SYMBOL(lprocfs_register);
651 EXPORT_SYMBOL(lprocfs_srch);
652 EXPORT_SYMBOL(lprocfs_remove);
653 EXPORT_SYMBOL(lprocfs_add_vars);
654 EXPORT_SYMBOL(lprocfs_obd_attach);
655 EXPORT_SYMBOL(lprocfs_obd_detach);
656 EXPORT_SYMBOL(lprocfs_alloc_stats);
657 EXPORT_SYMBOL(lprocfs_free_stats);
658 EXPORT_SYMBOL(lprocfs_register_stats);
659 EXPORT_SYMBOL(lprocfs_alloc_obd_stats);
660 EXPORT_SYMBOL(lprocfs_free_obd_stats);
662 EXPORT_SYMBOL(lprocfs_rd_u64);
663 EXPORT_SYMBOL(lprocfs_rd_uuid);
664 EXPORT_SYMBOL(lprocfs_rd_name);
665 EXPORT_SYMBOL(lprocfs_rd_fstype);
666 EXPORT_SYMBOL(lprocfs_rd_server_uuid);
667 EXPORT_SYMBOL(lprocfs_rd_conn_uuid);
668 EXPORT_SYMBOL(lprocfs_rd_numrefs);
670 EXPORT_SYMBOL(lprocfs_rd_blksize);
671 EXPORT_SYMBOL(lprocfs_rd_kbytestotal);
672 EXPORT_SYMBOL(lprocfs_rd_kbytesfree);
673 EXPORT_SYMBOL(lprocfs_rd_filestotal);
674 EXPORT_SYMBOL(lprocfs_rd_filesfree);
675 EXPORT_SYMBOL(lprocfs_rd_filegroups);