1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdd/mdd_lproc.c
38 * Lustre Metadata Server (mdd) routines
40 * Author: Wang Di <wangdi@clusterfs.com>
44 # define EXPORT_SYMTAB
46 #define DEBUG_SUBSYSTEM S_MDS
48 #include <linux/module.h>
49 #include <linux/poll.h>
51 #include <obd_class.h>
52 #include <lustre_ver.h>
53 #include <obd_support.h>
54 #include <lprocfs_status.h>
56 #include <lustre_log.h>
57 #include <lustre/lustre_idl.h>
58 #include <libcfs/libcfs_string.h>
60 #include "mdd_internal.h"
62 #ifndef SEEK_CUR /* SLES10 needs this */
67 static const char *mdd_counter_names[LPROC_MDD_NR] = {
70 int mdd_procfs_init(struct mdd_device *mdd, const char *name)
72 struct lprocfs_static_vars lvars;
73 struct lu_device *ld = &mdd->mdd_md_dev.md_lu_dev;
74 struct obd_type *type;
78 type = ld->ld_type->ldt_obd_type;
80 LASSERT(name != NULL);
81 LASSERT(type != NULL);
83 /* Find the type procroot and add the proc entry for this device */
84 lprocfs_mdd_init_vars(&lvars);
85 mdd->mdd_proc_entry = lprocfs_register(name, type->typ_procroot,
87 if (IS_ERR(mdd->mdd_proc_entry)) {
88 rc = PTR_ERR(mdd->mdd_proc_entry);
89 CERROR("Error %d setting up lprocfs for %s\n",
91 mdd->mdd_proc_entry = NULL;
95 rc = lu_time_init(&mdd->mdd_stats,
97 mdd_counter_names, ARRAY_SIZE(mdd_counter_names));
102 mdd_procfs_fini(mdd);
106 int mdd_procfs_fini(struct mdd_device *mdd)
109 lu_time_fini(&mdd->mdd_stats);
111 if (mdd->mdd_proc_entry) {
112 lprocfs_remove(&mdd->mdd_proc_entry);
113 mdd->mdd_proc_entry = NULL;
118 void mdd_lprocfs_time_start(const struct lu_env *env)
120 lu_lprocfs_time_start(env);
123 void mdd_lprocfs_time_end(const struct lu_env *env, struct mdd_device *mdd,
126 lu_lprocfs_time_end(env, mdd->mdd_stats, idx);
129 static int lprocfs_wr_atime_diff(struct file *file, const char *buffer,
130 unsigned long count, void *data)
132 struct mdd_device *mdd = data;
133 char kernbuf[20], *end;
134 unsigned long diff = 0;
136 if (count > (sizeof(kernbuf) - 1))
139 if (copy_from_user(kernbuf, buffer, count))
142 kernbuf[count] = '\0';
144 diff = simple_strtoul(kernbuf, &end, 0);
148 mdd->mdd_atime_diff = diff;
152 static int lprocfs_rd_atime_diff(char *page, char **start, off_t off,
153 int count, int *eof, void *data)
155 struct mdd_device *mdd = data;
158 return snprintf(page, count, "%lu\n", mdd->mdd_atime_diff);
162 /**** changelogs ****/
163 DECLARE_CHANGELOG_NAMES;
165 const char *changelog_bit2str(int bit)
168 return changelog_str[bit];
172 static int lprocfs_rd_changelog_mask(char *page, char **start, off_t off,
173 int count, int *eof, void *data)
175 struct mdd_device *mdd = data;
179 while (i < CL_LAST) {
180 if (mdd->mdd_cl.mc_mask & (1 << i))
181 rc += snprintf(page + rc, count - rc, "%s ",
188 static int lprocfs_wr_changelog_mask(struct file *file, const char *buffer,
189 unsigned long count, void *data)
191 struct mdd_device *mdd = data;
196 if (count >= CFS_PAGE_SIZE)
198 OBD_ALLOC(kernbuf, CFS_PAGE_SIZE);
201 if (copy_from_user(kernbuf, buffer, count))
202 GOTO(out, rc = -EFAULT);
205 rc = libcfs_str2mask(kernbuf, changelog_bit2str, &mdd->mdd_cl.mc_mask,
206 CHANGELOG_MINMASK, CHANGELOG_ALLMASK);
210 OBD_FREE(kernbuf, CFS_PAGE_SIZE);
220 static int lprocfs_changelog_users_cb(struct llog_handle *llh,
221 struct llog_rec_hdr *hdr, void *data)
223 struct llog_changelog_user_rec *rec;
224 struct cucb_data *cucb = (struct cucb_data *)data;
226 LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
228 rec = (struct llog_changelog_user_rec *)hdr;
230 cucb->idx += snprintf(cucb->page + cucb->idx, cucb->count - cucb->idx,
231 CHANGELOG_USER_PREFIX"%-3d "LPU64"\n",
232 rec->cur_id, rec->cur_endrec);
233 if (cucb->idx >= cucb->count)
239 static int lprocfs_rd_changelog_users(char *page, char **start, off_t off,
240 int count, int *eof, void *data)
242 struct mdd_device *mdd = data;
243 struct llog_ctxt *ctxt;
244 struct cucb_data cucb;
249 ctxt = llog_get_context(mdd2obd_dev(mdd),LLOG_CHANGELOG_USER_ORIG_CTXT);
252 LASSERT(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
254 spin_lock(&mdd->mdd_cl.mc_lock);
255 cur = mdd->mdd_cl.mc_index;
256 spin_unlock(&mdd->mdd_cl.mc_lock);
262 cucb.idx += snprintf(cucb.page + cucb.idx, cucb.count - cucb.idx,
263 "current index: "LPU64"\n", cur);
265 cucb.idx += snprintf(cucb.page + cucb.idx, cucb.count - cucb.idx,
266 "%-5s %s\n", "ID", "index");
268 llog_cat_process(ctxt->loc_handle, lprocfs_changelog_users_cb,
275 /* non-seq version for direct calling by class_process_proc_param */
276 static int mdd_changelog_write(struct file *file, const char *buffer,
277 unsigned long count, void *data)
279 struct mdd_device *mdd = (struct mdd_device *)data;
284 if (count > (sizeof(kernbuf) - 1))
287 count = min_t(unsigned long, count, sizeof(kernbuf));
288 if (copy_from_user(kernbuf, buffer, count))
291 kernbuf[count] = '\0';
292 /* strip trailing newline from "echo blah" */
293 if (kernbuf[count - 1] == '\n')
294 kernbuf[count - 1] = '\0';
296 if (strcmp(kernbuf, "on") == 0) {
297 LCONSOLE_INFO("changelog on\n");
298 if (mdd->mdd_cl.mc_flags & CLM_ERR) {
299 CERROR("Changelogs cannot be enabled due to error "
302 spin_lock(&mdd->mdd_cl.mc_lock);
303 mdd->mdd_cl.mc_flags |= CLM_ON;
304 spin_unlock(&mdd->mdd_cl.mc_lock);
305 rc = mdd_changelog_write_header(mdd, CLM_START);
309 } else if (strcmp(kernbuf, "off") == 0) {
310 LCONSOLE_INFO("changelog off\n");
311 rc = mdd_changelog_write_header(mdd, CLM_FINI);
314 spin_lock(&mdd->mdd_cl.mc_lock);
315 mdd->mdd_cl.mc_flags &= ~CLM_ON;
316 spin_unlock(&mdd->mdd_cl.mc_lock);
318 /* purge to an index */
319 long long unsigned endrec;
321 endrec = (long long)simple_strtoull(kernbuf, &end, 0);
325 LCONSOLE_INFO("changelog purge to %llu\n", endrec);
327 rc = mdd_changelog_llog_cancel(mdd, endrec);
335 CWARN("changelog write usage: [on|off] | <purge_idx (0=all)>\n");
339 static ssize_t mdd_changelog_seq_write(struct file *file, const char *buffer,
340 size_t count, loff_t *off)
342 struct seq_file *seq = file->private_data;
343 struct changelog_seq_iter *csi = seq->private;
344 struct mdd_device *mdd = (struct mdd_device *)csi->csi_dev;
346 return mdd_changelog_write(file, buffer, count, mdd);
349 static int mdd_changelog_done(struct changelog_seq_iter *csi)
351 struct mdd_device *mdd = (struct mdd_device *)csi->csi_dev;
354 spin_lock(&mdd->mdd_cl.mc_lock);
355 done = (csi->csi_endrec >= mdd->mdd_cl.mc_index);
356 spin_unlock(&mdd->mdd_cl.mc_lock);
360 /* handle nonblocking */
361 static ssize_t mdd_changelog_seq_read(struct file *file, char __user *buf,
362 size_t count, loff_t *ppos)
364 struct seq_file *seq = (struct seq_file *)file->private_data;
365 struct changelog_seq_iter *csi = seq->private;
369 if ((file->f_flags & O_NONBLOCK) && mdd_changelog_done(csi))
373 rc = seq_read(file, buf, count, ppos);
377 /* handle nonblocking */
378 static unsigned int mdd_changelog_seq_poll(struct file *file, poll_table *wait)
380 struct seq_file *seq = (struct seq_file *)file->private_data;
381 struct changelog_seq_iter *csi = seq->private;
382 struct mdd_device *mdd = (struct mdd_device *)csi->csi_dev;
386 poll_wait(file, &mdd->mdd_cl.mc_waitq, wait);
387 if (!mdd_changelog_done(csi))
388 RETURN(POLLIN | POLLRDNORM);
393 static int mdd_changelog_seq_open(struct inode *inode, struct file *file)
395 struct changelog_seq_iter *csi;
396 struct obd_device *obd;
400 rc = changelog_seq_open(inode, file, &csi);
404 /* The proc file is set up with mdd in data, not obd */
405 obd = mdd2obd_dev((struct mdd_device *)csi->csi_dev);
406 csi->csi_ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
407 if (csi->csi_ctxt == NULL) {
408 changelog_seq_release(inode, file);
411 /* The handle is set up in llog_obd_origin_setup */
412 csi->csi_llh = csi->csi_ctxt->loc_handle;
416 static int mdd_changelog_seq_release(struct inode *inode, struct file *file)
418 struct seq_file *seq = file->private_data;
419 struct changelog_seq_iter *csi = seq->private;
421 if (csi && csi->csi_ctxt)
422 llog_ctxt_put(csi->csi_ctxt);
424 return (changelog_seq_release(inode, file));
427 /* mdd changelog proc can handle nonblocking ops and writing to purge recs */
428 struct file_operations mdd_changelog_fops = {
429 .owner = THIS_MODULE,
430 .open = mdd_changelog_seq_open,
431 .read = mdd_changelog_seq_read,
432 .write = mdd_changelog_seq_write,
433 .llseek = changelog_seq_lseek,
434 .poll = mdd_changelog_seq_poll,
435 .release = mdd_changelog_seq_release,
438 #ifdef HAVE_QUOTA_SUPPORT
439 static int mdd_lprocfs_quota_rd_type(char *page, char **start, off_t off,
440 int count, int *eof, void *data)
442 struct mdd_device *mdd = data;
443 return lprocfs_quota_rd_type(page, start, off, count, eof,
447 static int mdd_lprocfs_quota_wr_type(struct file *file, const char *buffer,
448 unsigned long count, void *data)
450 struct mdd_device *mdd = data;
451 return lprocfs_quota_wr_type(file, buffer, count, mdd->mdd_obd_dev);
455 static struct lprocfs_vars lprocfs_mdd_obd_vars[] = {
456 { "atime_diff", lprocfs_rd_atime_diff, lprocfs_wr_atime_diff, 0 },
457 { "changelog_mask", lprocfs_rd_changelog_mask,
458 lprocfs_wr_changelog_mask, 0 },
459 { "changelog_users", lprocfs_rd_changelog_users, 0, 0},
460 { "changelog", 0, mdd_changelog_write, 0, &mdd_changelog_fops, 0600 },
461 #ifdef HAVE_QUOTA_SUPPORT
462 { "quota_type", mdd_lprocfs_quota_rd_type,
463 mdd_lprocfs_quota_wr_type, 0 },
468 static struct lprocfs_vars lprocfs_mdd_module_vars[] = {
469 { "num_refs", lprocfs_rd_numrefs, 0, 0 },
473 void lprocfs_mdd_init_vars(struct lprocfs_static_vars *lvars)
475 lvars->module_vars = lprocfs_mdd_module_vars;
476 lvars->obd_vars = lprocfs_mdd_obd_vars;