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 static const char *mdd_counter_names[LPROC_MDD_NR] = {
65 int mdd_procfs_init(struct mdd_device *mdd, const char *name)
67 struct lprocfs_static_vars lvars;
68 struct lu_device *ld = &mdd->mdd_md_dev.md_lu_dev;
69 struct obd_type *type;
73 type = ld->ld_type->ldt_obd_type;
75 LASSERT(name != NULL);
76 LASSERT(type != NULL);
78 /* Find the type procroot and add the proc entry for this device */
79 lprocfs_mdd_init_vars(&lvars);
80 mdd->mdd_proc_entry = lprocfs_register(name, type->typ_procroot,
82 if (IS_ERR(mdd->mdd_proc_entry)) {
83 rc = PTR_ERR(mdd->mdd_proc_entry);
84 CERROR("Error %d setting up lprocfs for %s\n",
86 mdd->mdd_proc_entry = NULL;
90 rc = lu_time_init(&mdd->mdd_stats,
92 mdd_counter_names, ARRAY_SIZE(mdd_counter_names));
101 int mdd_procfs_fini(struct mdd_device *mdd)
104 lu_time_fini(&mdd->mdd_stats);
106 if (mdd->mdd_proc_entry) {
107 lprocfs_remove(&mdd->mdd_proc_entry);
108 mdd->mdd_proc_entry = NULL;
113 void mdd_lprocfs_time_start(const struct lu_env *env)
115 lu_lprocfs_time_start(env);
118 void mdd_lprocfs_time_end(const struct lu_env *env, struct mdd_device *mdd,
121 lu_lprocfs_time_end(env, mdd->mdd_stats, idx);
124 static int lprocfs_wr_atime_diff(struct file *file, const char *buffer,
125 unsigned long count, void *data)
127 struct mdd_device *mdd = data;
128 char kernbuf[20], *end;
129 unsigned long diff = 0;
131 if (count > (sizeof(kernbuf) - 1))
134 if (copy_from_user(kernbuf, buffer, count))
137 kernbuf[count] = '\0';
139 diff = simple_strtoul(kernbuf, &end, 0);
143 mdd->mdd_atime_diff = diff;
147 static int lprocfs_rd_atime_diff(char *page, char **start, off_t off,
148 int count, int *eof, void *data)
150 struct mdd_device *mdd = data;
153 return snprintf(page, count, "%lu\n", mdd->mdd_atime_diff);
157 /**** changelogs ****/
158 static int lprocfs_rd_changelog_mask(char *page, char **start, off_t off,
159 int count, int *eof, void *data)
161 struct mdd_device *mdd = data;
165 while (i < CL_LAST) {
166 if (mdd->mdd_cl.mc_mask & (1 << i))
167 rc += snprintf(page + rc, count - rc, "%s ",
168 changelog_type2str(i));
174 static int lprocfs_wr_changelog_mask(struct file *file, const char *buffer,
175 unsigned long count, void *data)
177 struct mdd_device *mdd = data;
182 if (count >= CFS_PAGE_SIZE)
184 OBD_ALLOC(kernbuf, CFS_PAGE_SIZE);
187 if (copy_from_user(kernbuf, buffer, count))
188 GOTO(out, rc = -EFAULT);
191 rc = libcfs_str2mask(kernbuf, changelog_type2str, &mdd->mdd_cl.mc_mask,
192 CHANGELOG_MINMASK, CHANGELOG_ALLMASK);
196 OBD_FREE(kernbuf, CFS_PAGE_SIZE);
206 static int lprocfs_changelog_users_cb(struct llog_handle *llh,
207 struct llog_rec_hdr *hdr, void *data)
209 struct llog_changelog_user_rec *rec;
210 struct cucb_data *cucb = (struct cucb_data *)data;
212 LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
214 rec = (struct llog_changelog_user_rec *)hdr;
216 cucb->idx += snprintf(cucb->page + cucb->idx, cucb->count - cucb->idx,
217 CHANGELOG_USER_PREFIX"%-3d "LPU64"\n",
218 rec->cur_id, rec->cur_endrec);
219 if (cucb->idx >= cucb->count)
225 static int lprocfs_rd_changelog_users(char *page, char **start, off_t off,
226 int count, int *eof, void *data)
228 struct mdd_device *mdd = data;
229 struct llog_ctxt *ctxt;
230 struct cucb_data cucb;
235 ctxt = llog_get_context(mdd2obd_dev(mdd),LLOG_CHANGELOG_USER_ORIG_CTXT);
238 LASSERT(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
240 spin_lock(&mdd->mdd_cl.mc_lock);
241 cur = mdd->mdd_cl.mc_index;
242 spin_unlock(&mdd->mdd_cl.mc_lock);
248 cucb.idx += snprintf(cucb.page + cucb.idx, cucb.count - cucb.idx,
249 "current index: "LPU64"\n", cur);
251 cucb.idx += snprintf(cucb.page + cucb.idx, cucb.count - cucb.idx,
252 "%-5s %s\n", "ID", "index");
254 llog_cat_process(ctxt->loc_handle, lprocfs_changelog_users_cb,
261 /* non-seq version for direct calling by class_process_proc_param */
262 static int mdd_changelog_write(struct file *file, const char *buffer,
263 unsigned long count, void *data)
265 struct mdd_device *mdd = (struct mdd_device *)data;
270 if (count > (sizeof(kernbuf) - 1))
273 count = min_t(unsigned long, count, sizeof(kernbuf));
274 if (copy_from_user(kernbuf, buffer, count))
277 kernbuf[count] = '\0';
278 /* strip trailing newline from "echo blah" */
279 if (kernbuf[count - 1] == '\n')
280 kernbuf[count - 1] = '\0';
282 /* Forced on/off/purge rec, independent of changelog users! */
283 if (strcmp(kernbuf, "on") == 0) {
284 rc = mdd_changelog_on(mdd, 1);
285 } else if (strcmp(kernbuf, "off") == 0) {
286 rc = mdd_changelog_on(mdd, 0);
288 /* purge to an index */
289 long long unsigned endrec;
291 endrec = (long long)simple_strtoull(kernbuf, &end, 0);
295 LCONSOLE_INFO("changelog purge to %llu\n", endrec);
297 rc = mdd_changelog_llog_cancel(mdd, endrec);
305 CWARN("changelog write usage: [on|off] | <purge_idx (0=all)>\n");
309 static ssize_t mdd_changelog_seq_write(struct file *file, const char *buffer,
310 size_t count, loff_t *off)
312 struct seq_file *seq = file->private_data;
313 struct changelog_seq_iter *csi = seq->private;
314 struct mdd_device *mdd = (struct mdd_device *)csi->csi_dev;
316 return mdd_changelog_write(file, buffer, count, mdd);
319 static int mdd_changelog_done(struct changelog_seq_iter *csi)
321 struct mdd_device *mdd = (struct mdd_device *)csi->csi_dev;
324 spin_lock(&mdd->mdd_cl.mc_lock);
325 done = (csi->csi_endrec >= mdd->mdd_cl.mc_index);
326 spin_unlock(&mdd->mdd_cl.mc_lock);
330 /* handle nonblocking */
331 static ssize_t mdd_changelog_seq_read(struct file *file, char __user *buf,
332 size_t count, loff_t *ppos)
334 struct seq_file *seq = (struct seq_file *)file->private_data;
335 struct changelog_seq_iter *csi = seq->private;
339 if ((file->f_flags & O_NONBLOCK) && mdd_changelog_done(csi))
343 rc = seq_read(file, buf, count, ppos);
347 /* handle nonblocking */
348 static unsigned int mdd_changelog_seq_poll(struct file *file, poll_table *wait)
350 struct seq_file *seq = (struct seq_file *)file->private_data;
351 struct changelog_seq_iter *csi = seq->private;
352 struct mdd_device *mdd = (struct mdd_device *)csi->csi_dev;
356 poll_wait(file, &mdd->mdd_cl.mc_waitq, wait);
357 if (!mdd_changelog_done(csi))
358 RETURN(POLLIN | POLLRDNORM);
363 static int mdd_changelog_seq_open(struct inode *inode, struct file *file)
365 struct changelog_seq_iter *csi;
366 struct obd_device *obd;
370 rc = changelog_seq_open(inode, file, &csi);
374 /* The proc file is set up with mdd in data, not obd */
375 obd = mdd2obd_dev((struct mdd_device *)csi->csi_dev);
376 csi->csi_ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
377 if (csi->csi_ctxt == NULL) {
378 changelog_seq_release(inode, file);
381 /* The handle is set up in llog_obd_origin_setup */
382 csi->csi_llh = csi->csi_ctxt->loc_handle;
386 static int mdd_changelog_seq_release(struct inode *inode, struct file *file)
388 struct seq_file *seq = file->private_data;
389 struct changelog_seq_iter *csi = seq->private;
391 if (csi && csi->csi_ctxt)
392 llog_ctxt_put(csi->csi_ctxt);
394 return (changelog_seq_release(inode, file));
397 /* mdd changelog proc can handle nonblocking ops and writing to purge recs */
398 struct file_operations mdd_changelog_fops = {
399 .owner = THIS_MODULE,
400 .open = mdd_changelog_seq_open,
401 .read = mdd_changelog_seq_read,
402 .write = mdd_changelog_seq_write,
403 .llseek = changelog_seq_lseek,
404 .poll = mdd_changelog_seq_poll,
405 .release = mdd_changelog_seq_release,
408 #ifdef HAVE_QUOTA_SUPPORT
409 static int mdd_lprocfs_quota_rd_type(char *page, char **start, off_t off,
410 int count, int *eof, void *data)
412 struct mdd_device *mdd = data;
413 return lprocfs_quota_rd_type(page, start, off, count, eof,
417 static int mdd_lprocfs_quota_wr_type(struct file *file, const char *buffer,
418 unsigned long count, void *data)
420 struct mdd_device *mdd = data;
421 return lprocfs_quota_wr_type(file, buffer, count, mdd->mdd_obd_dev);
425 static struct lprocfs_vars lprocfs_mdd_obd_vars[] = {
426 { "atime_diff", lprocfs_rd_atime_diff, lprocfs_wr_atime_diff, 0 },
427 { "changelog_mask", lprocfs_rd_changelog_mask,
428 lprocfs_wr_changelog_mask, 0 },
429 { "changelog_users", lprocfs_rd_changelog_users, 0, 0},
430 { "changelog", 0, mdd_changelog_write, 0, &mdd_changelog_fops, 0600 },
431 #ifdef HAVE_QUOTA_SUPPORT
432 { "quota_type", mdd_lprocfs_quota_rd_type,
433 mdd_lprocfs_quota_wr_type, 0 },
438 static struct lprocfs_vars lprocfs_mdd_module_vars[] = {
439 { "num_refs", lprocfs_rd_numrefs, 0, 0 },
443 void lprocfs_mdd_init_vars(struct lprocfs_static_vars *lvars)
445 lvars->module_vars = lprocfs_mdd_module_vars;
446 lvars->obd_vars = lprocfs_mdd_obd_vars;