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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/mdd/mdd_lproc.c
33 * Lustre Metadata Server (mdd) routines
35 * Author: Wang Di <wangdi@clusterfs.com>
38 #define DEBUG_SUBSYSTEM S_MDS
41 #include <obd_class.h>
42 #include <obd_support.h>
43 #include <lprocfs_status.h>
44 #include <libcfs/libcfs_string.h>
45 #include "mdd_internal.h"
47 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
50 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
52 struct obd_device *obd = mdd2obd_dev(mdd);
54 return sprintf(buf, "%s\n", obd->obd_uuid.uuid);
58 static ssize_t atime_diff_show(struct kobject *kobj, struct attribute *attr,
61 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
64 return sprintf(buf, "%lld\n", mdd->mdd_atime_diff);
67 static ssize_t atime_diff_store(struct kobject *kobj,
68 struct attribute *attr,
69 const char *buffer, size_t count)
71 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
76 rc = kstrtoll(buffer, 10, &diff);
80 mdd->mdd_atime_diff = diff;
83 LUSTRE_RW_ATTR(atime_diff);
85 /**** changelogs ****/
86 static int mdd_changelog_current_mask_seq_show(struct seq_file *m, void *data)
88 struct mdd_device *mdd = m->private;
92 if (mdd->mdd_cl.mc_current_mask & BIT(i))
93 seq_printf(m, "%s ", changelog_type2str(i));
99 LDEBUGFS_SEQ_FOPS_RO(mdd_changelog_current_mask);
101 static int mdd_changelog_mask_seq_show(struct seq_file *m, void *data)
103 struct mdd_device *mdd = m->private;
106 while (i < CL_LAST) {
107 if (mdd->mdd_cl.mc_proc_mask & BIT(i))
108 seq_printf(m, "%s ", changelog_type2str(i));
116 mdd_changelog_mask_seq_write(struct file *file, const char __user *buffer,
117 size_t count, loff_t *off)
119 struct seq_file *m = file->private_data;
120 struct mdd_device *mdd = m->private;
123 int oldmask = mdd->mdd_cl.mc_proc_mask;
124 int newmask = oldmask;
128 if (count >= PAGE_SIZE)
130 OBD_ALLOC(kernbuf, PAGE_SIZE);
133 if (copy_from_user(kernbuf, buffer, count))
134 GOTO(out, rc = -EFAULT);
137 /* if the new mask is relative and proc mask is minimal then assume
138 * it is relative to DEFMASK, otherwise apply new mask on the current
141 if (oldmask == CHANGELOG_MINMASK) {
144 while (isspace(*str))
146 if (*str == '+' || *str == '-')
147 newmask = CHANGELOG_DEFMASK;
150 rc = cfs_str2mask(kernbuf, changelog_type2str, &newmask,
151 CHANGELOG_MINMASK, CHANGELOG_ALLMASK);
155 mdd->mdd_cl.mc_proc_mask = newmask;
157 /* if mask keeps all bits from oldmask then just extend the current
158 * mask, otherwise the current mask should be recalculated through
161 if ((newmask & oldmask) == oldmask) {
162 spin_lock(&mdd->mdd_cl.mc_user_lock);
163 mdd->mdd_cl.mc_current_mask |= newmask;
164 spin_unlock(&mdd->mdd_cl.mc_user_lock);
168 rc = lu_env_init(&env, LCT_LOCAL);
172 mdd_changelog_recalc_mask(&env, mdd);
180 OBD_FREE(kernbuf, PAGE_SIZE);
183 LDEBUGFS_SEQ_FOPS(mdd_changelog_mask);
185 static int lprocfs_changelog_users_cb(const struct lu_env *env,
186 struct llog_handle *llh,
187 struct llog_rec_hdr *hdr, void *data)
189 struct llog_changelog_user_rec2 *rec;
190 struct seq_file *m = data;
191 char user_name[CHANGELOG_USER_NAMELEN_FULL];
193 LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
195 rec = container_of(hdr, typeof(*rec), cur_hdr);
197 seq_printf(m, "%-24s %10llu (%u)",
198 mdd_chlg_username(rec, user_name, sizeof(user_name)),
200 (__u32)ktime_get_real_seconds() - rec->cur_time);
201 if (mdd_chlg_usermask(rec)) {
205 seq_puts(m, " mask=");
206 for (i = 0; i < CL_LAST; i++) {
207 if (!(mdd_chlg_usermask(rec) & BIT(i)))
211 seq_puts(m, changelog_type2str(i));
220 static int mdd_changelog_users_seq_show(struct seq_file *m, void *data)
223 struct mdd_device *mdd = m->private;
224 struct llog_ctxt *ctxt;
228 ctxt = llog_get_context(mdd2obd_dev(mdd),
229 LLOG_CHANGELOG_USER_ORIG_CTXT);
232 LASSERT(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
234 rc = lu_env_init(&env, LCT_LOCAL);
240 spin_lock(&mdd->mdd_cl.mc_lock);
241 cur = mdd->mdd_cl.mc_index;
242 spin_unlock(&mdd->mdd_cl.mc_lock);
244 seq_printf(m, "current_index: %llu\n", cur);
245 seq_printf(m, "%-24s %10s %s %s\n", "ID", "index", "(idle)", "mask");
247 llog_cat_process(&env, ctxt->loc_handle, lprocfs_changelog_users_cb,
254 LDEBUGFS_SEQ_FOPS_RO(mdd_changelog_users);
256 static int mdd_changelog_size_ctxt(const struct lu_env *env,
257 struct mdd_device *mdd,
258 int index, __u64 *val)
260 struct llog_ctxt *ctxt;
262 ctxt = llog_get_context(mdd2obd_dev(mdd),
267 if (!(ctxt->loc_handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
268 CERROR("%s: ChangeLog has wrong flags: rc = %d\n",
269 ctxt->loc_obd->obd_name, -EINVAL);
274 *val += llog_cat_size(env, ctxt->loc_handle);
281 static ssize_t changelog_size_show(struct kobject *kobj,
282 struct attribute *attr,
285 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
291 rc = lu_env_init(&env, LCT_LOCAL);
295 rc = mdd_changelog_size_ctxt(&env, mdd, LLOG_CHANGELOG_ORIG_CTXT, &tmp);
301 rc = mdd_changelog_size_ctxt(&env, mdd, LLOG_CHANGELOG_USER_ORIG_CTXT,
304 rc = sprintf(buf, "%llu\n", tmp);
308 LUSTRE_RO_ATTR(changelog_size);
310 static ssize_t changelog_gc_show(struct kobject *kobj,
311 struct attribute *attr,
314 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
317 return sprintf(buf, "%u\n", mdd->mdd_changelog_gc);
320 static ssize_t changelog_gc_store(struct kobject *kobj,
321 struct attribute *attr,
322 const char *buffer, size_t count)
324 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
329 rc = kstrtobool(buffer, &val);
333 mdd->mdd_changelog_gc = val;
337 LUSTRE_RW_ATTR(changelog_gc);
339 static ssize_t changelog_max_idle_time_show(struct kobject *kobj,
340 struct attribute *attr,
343 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
346 return sprintf(buf, "%lld\n", mdd->mdd_changelog_max_idle_time);
349 static ssize_t changelog_max_idle_time_store(struct kobject *kobj,
350 struct attribute *attr,
351 const char *buffer, size_t count)
353 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
358 rc = kstrtoll(buffer, 10, &val);
362 /* as it sounds reasonable, do not allow a user to be idle since
363 * more than about 68 years, this will allow to use 32bits
364 * timestamps for comparison
366 if (val < 1 || val > INT_MAX)
369 mdd->mdd_changelog_max_idle_time = val;
373 LUSTRE_RW_ATTR(changelog_max_idle_time);
375 static ssize_t changelog_max_idle_indexes_show(struct kobject *kobj,
376 struct attribute *attr,
379 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
382 return sprintf(buf, "%lu\n", mdd->mdd_changelog_max_idle_indexes);
385 static ssize_t changelog_max_idle_indexes_store(struct kobject *kobj,
386 struct attribute *attr,
390 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
395 LASSERT(mdd != NULL);
396 rc = kstrtoul(buffer, 0, &val);
400 /* XXX may need to limit/check with reasonable elapsed/idle indexes */
401 /* XXX may better allow to specify a % of full ChangeLogs */
403 mdd->mdd_changelog_max_idle_indexes = val;
407 LUSTRE_RW_ATTR(changelog_max_idle_indexes);
409 static ssize_t changelog_min_gc_interval_show(struct kobject *kobj,
410 struct attribute *attr,
413 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
416 return sprintf(buf, "%lld\n", mdd->mdd_changelog_min_gc_interval);
419 static ssize_t changelog_min_gc_interval_store(struct kobject *kobj,
420 struct attribute *attr,
424 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
429 rc = kstrtoll(buffer, 10, &val);
433 /* XXX may need to limit with reasonable elapsed/interval times */
437 mdd->mdd_changelog_min_gc_interval = val;
441 LUSTRE_RW_ATTR(changelog_min_gc_interval);
443 static ssize_t changelog_min_free_cat_entries_show(struct kobject *kobj,
444 struct attribute *attr,
447 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
450 return sprintf(buf, "%u\n", mdd->mdd_changelog_min_free_cat_entries);
453 static ssize_t changelog_min_free_cat_entries_store(struct kobject *kobj,
454 struct attribute *attr,
458 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
463 rc = kstrtouint(buffer, 10, &val);
467 /* XXX may need to limit with more reasonable number of free entries */
471 mdd->mdd_changelog_min_free_cat_entries = val;
475 LUSTRE_RW_ATTR(changelog_min_free_cat_entries);
477 static ssize_t changelog_deniednext_show(struct kobject *kobj,
478 struct attribute *attr,
481 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
484 return sprintf(buf, "%u\n", mdd->mdd_cl.mc_deniednext);
487 static ssize_t changelog_deniednext_store(struct kobject *kobj,
488 struct attribute *attr,
492 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
494 unsigned int time = 0;
497 rc = kstrtouint(buffer, 0, &time);
501 mdd->mdd_cl.mc_deniednext = time;
504 LUSTRE_RW_ATTR(changelog_deniednext);
506 static ssize_t sync_permission_show(struct kobject *kobj,
507 struct attribute *attr, char *buf)
509 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
512 return sprintf(buf, "%d\n", mdd->mdd_sync_permission);
515 static ssize_t sync_permission_store(struct kobject *kobj,
516 struct attribute *attr,
517 const char *buffer, size_t count)
519 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
524 rc = kstrtobool(buffer, &val);
528 mdd->mdd_sync_permission = val;
532 LUSTRE_RW_ATTR(sync_permission);
534 static ssize_t lfsck_speed_limit_show(struct kobject *kobj,
535 struct attribute *attr, char *buf)
537 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
540 return lfsck_get_speed(buf, mdd->mdd_bottom);
543 static ssize_t lfsck_speed_limit_store(struct kobject *kobj,
544 struct attribute *attr,
545 const char *buffer, size_t count)
547 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
552 rc = kstrtouint(buffer, 10, &val);
556 rc = lfsck_set_speed(mdd->mdd_bottom, val);
557 return rc != 0 ? rc : count;
559 LUSTRE_RW_ATTR(lfsck_speed_limit);
561 static ssize_t lfsck_async_windows_show(struct kobject *kobj,
562 struct attribute *attr, char *buf)
564 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
567 return lfsck_get_windows(buf, mdd->mdd_bottom);
570 static ssize_t lfsck_async_windows_store(struct kobject *kobj,
571 struct attribute *attr,
572 const char *buffer, size_t count)
574 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
579 rc = kstrtouint(buffer, 10, &val);
583 rc = lfsck_set_windows(mdd->mdd_bottom, val);
585 return rc != 0 ? rc : count;
587 LUSTRE_RW_ATTR(lfsck_async_windows);
589 static int mdd_lfsck_namespace_seq_show(struct seq_file *m, void *data)
591 struct mdd_device *mdd = m->private;
593 LASSERT(mdd != NULL);
595 return lfsck_dump(m, mdd->mdd_bottom, LFSCK_TYPE_NAMESPACE);
597 LDEBUGFS_SEQ_FOPS_RO(mdd_lfsck_namespace);
599 static int mdd_lfsck_layout_seq_show(struct seq_file *m, void *data)
601 struct mdd_device *mdd = m->private;
603 LASSERT(mdd != NULL);
605 return lfsck_dump(m, mdd->mdd_bottom, LFSCK_TYPE_LAYOUT);
607 LDEBUGFS_SEQ_FOPS_RO(mdd_lfsck_layout);
610 * Show default number of stripes for O_APPEND files.
612 * \param[in] m seq file
613 * \param[in] v unused for single entry
615 * \retval 0 on success,
616 * \retval negative error code if failed
618 static ssize_t append_stripe_count_show(struct kobject *kobj,
619 struct attribute *attr, char *buf)
621 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
624 return scnprintf(buf, PAGE_SIZE, "%d\n", mdd->mdd_append_stripe_count);
628 * Set default number of stripes for O_APPEND files.
630 * \param[in] file proc file
631 * \param[in] buffer string containing the default number of stripes
633 * \param[in] count @buffer length
634 * \param[in] off unused for single entry
636 * \retval @count on success
637 * \retval negative error code otherwise
639 static ssize_t append_stripe_count_store(struct kobject *kobj,
640 struct attribute *attr,
641 const char *buffer, size_t count)
643 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
648 rc = kstrtoint(buffer, 0, &stripe_count);
652 if (stripe_count < -1)
655 mdd->mdd_append_stripe_count = stripe_count;
659 LUSTRE_RW_ATTR(append_stripe_count);
662 * Show default OST pool for O_APPEND files.
664 * \param[in] kobject proc object
665 * \param[in] attribute proc attribute
666 * \param[in] buf output buffer
668 * \retval 0 on success,
669 * \retval negative error code if failed
671 static ssize_t append_pool_show(struct kobject *kobj,
672 struct attribute *attr, char *buf)
674 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
677 return scnprintf(buf, PAGE_SIZE, "%s\n", mdd->mdd_append_pool);
681 * Set default OST pool for O_APPEND files.
683 * \param[in] kobject proc object
684 * \param[in] attribute proc attribute
685 * \param[in] buffer user inputted pool name
686 * \param[in] count @buffer length
688 * \retval @count on success
689 * \retval negative error code otherwise
691 static ssize_t append_pool_store(struct kobject *kobj, struct attribute *attr,
692 const char *buffer, size_t count)
694 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
697 if (!count || count > LOV_MAXPOOLNAME + 1)
700 /* clear previous value */
701 memset(mdd->mdd_append_pool, 0, LOV_MAXPOOLNAME + 1);
703 /* entering "none" clears the pool, otherwise copy the new pool */
704 if (strncmp("none", buffer, 4)) {
705 memcpy(mdd->mdd_append_pool, buffer, count);
707 /* Trim the trailing '\n' if any */
708 if (mdd->mdd_append_pool[count - 1] == '\n') {
709 /* Don't echo just a newline */
712 mdd->mdd_append_pool[count - 1] = 0;
718 LUSTRE_RW_ATTR(append_pool);
720 static struct ldebugfs_vars ldebugfs_mdd_obd_vars[] = {
721 { .name = "changelog_mask",
722 .fops = &mdd_changelog_mask_fops },
723 { .name = "changelog_current_mask",
724 .fops = &mdd_changelog_current_mask_fops },
725 { .name = "changelog_users",
726 .fops = &mdd_changelog_users_fops },
727 { .name = "lfsck_namespace",
728 .fops = &mdd_lfsck_namespace_fops },
729 { .name = "lfsck_layout",
730 .fops = &mdd_lfsck_layout_fops },
734 static struct attribute *mdd_attrs[] = {
735 &lustre_attr_uuid.attr,
736 &lustre_attr_atime_diff.attr,
737 &lustre_attr_changelog_size.attr,
738 &lustre_attr_changelog_gc.attr,
739 &lustre_attr_changelog_max_idle_time.attr,
740 &lustre_attr_changelog_max_idle_indexes.attr,
741 &lustre_attr_changelog_min_gc_interval.attr,
742 &lustre_attr_changelog_min_free_cat_entries.attr,
743 &lustre_attr_changelog_deniednext.attr,
744 &lustre_attr_lfsck_async_windows.attr,
745 &lustre_attr_lfsck_speed_limit.attr,
746 &lustre_attr_sync_permission.attr,
747 &lustre_attr_append_stripe_count.attr,
748 &lustre_attr_append_pool.attr,
752 static void mdd_sysfs_release(struct kobject *kobj)
754 struct mdd_device *mdd = container_of(kobj, struct mdd_device,
756 struct obd_device *obd = mdd2obd_dev(mdd);
758 debugfs_remove_recursive(obd->obd_debugfs_entry);
759 obd->obd_debugfs_entry = NULL;
761 complete(&mdd->mdd_kobj_unregister);
764 int mdd_procfs_init(struct mdd_device *mdd, const char *name)
766 struct obd_device *obd = mdd2obd_dev(mdd);
767 struct obd_type *type;
771 /* at the moment there is no linkage between lu_type
772 * and obd_type, so we lookup obd_type this way
774 type = class_search_type(LUSTRE_MDD_NAME);
776 LASSERT(name != NULL);
777 LASSERT(type != NULL);
778 LASSERT(obd != NULL);
780 /* put reference taken by class_search_type */
781 kobject_put(&type->typ_kobj);
783 mdd->mdd_ktype.default_attrs = mdd_attrs;
784 mdd->mdd_ktype.release = mdd_sysfs_release;
785 mdd->mdd_ktype.sysfs_ops = &lustre_sysfs_ops;
787 init_completion(&mdd->mdd_kobj_unregister);
788 rc = kobject_init_and_add(&mdd->mdd_kobj, &mdd->mdd_ktype,
789 &type->typ_kobj, "%s", name);
793 /* Find the type procroot and add the proc entry for this device */
794 obd->obd_debugfs_vars = ldebugfs_mdd_obd_vars;
795 obd->obd_debugfs_entry = debugfs_create_dir(name,
796 type->typ_debugfs_entry);
797 ldebugfs_add_vars(obd->obd_debugfs_entry, obd->obd_debugfs_vars, mdd);
802 void mdd_procfs_fini(struct mdd_device *mdd)
804 kobject_put(&mdd->mdd_kobj);
805 wait_for_completion(&mdd->mdd_kobj_unregister);