1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
4 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/sched.h>
10 #include <linux/cred.h>
11 #include <linux/spinlock.h>
12 #include <linux/completion.h>
13 #include <linux/buffer_head.h>
14 #include <linux/module.h>
15 #include <linux/kobject.h>
16 #include <linux/uaccess.h>
17 #include <linux/gfs2_ondisk.h>
18 #include <linux/blkdev.h>
31 struct attribute attr;
32 ssize_t (*show)(struct gfs2_sbd *, char *);
33 ssize_t (*store)(struct gfs2_sbd *, const char *, size_t);
36 static ssize_t gfs2_attr_show(struct kobject *kobj, struct attribute *attr,
39 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
40 struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
41 return a->show ? a->show(sdp, buf) : 0;
44 static ssize_t gfs2_attr_store(struct kobject *kobj, struct attribute *attr,
45 const char *buf, size_t len)
47 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
48 struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
49 return a->store ? a->store(sdp, buf, len) : len;
52 static const struct sysfs_ops gfs2_attr_ops = {
53 .show = gfs2_attr_show,
54 .store = gfs2_attr_store,
58 static struct kset *gfs2_kset;
60 static ssize_t id_show(struct gfs2_sbd *sdp, char *buf)
62 return snprintf(buf, PAGE_SIZE, "%u:%u\n",
63 MAJOR(sdp->sd_vfs->s_dev), MINOR(sdp->sd_vfs->s_dev));
66 static ssize_t status_show(struct gfs2_sbd *sdp, char *buf)
68 unsigned long f = sdp->sd_flags;
71 s = snprintf(buf, PAGE_SIZE,
72 "Journal Checked: %d\n"
83 "Skip DLM Unlock: %d\n"
84 "Force AIL Flush: %d\n"
87 "Withdraw In Prog: %d\n"
88 "Remote Withdraw: %d\n"
89 "Withdraw Recovery: %d\n"
91 "sd_log_flush_lock: %d\n"
92 "sd_log_num_revoke: %u\n"
93 "sd_log_in_flight: %d\n"
94 "sd_log_blks_needed: %d\n"
95 "sd_log_blks_free: %d\n"
96 "sd_log_flush_head: %d\n"
97 "sd_log_flush_tail: %d\n"
98 "sd_log_blks_reserved: %d\n"
99 "sd_log_revokes_available: %d\n",
100 test_bit(SDF_JOURNAL_CHECKED, &f),
101 test_bit(SDF_JOURNAL_LIVE, &f),
102 (sdp->sd_jdesc ? sdp->sd_jdesc->jd_jid : 0),
103 (sdp->sd_args.ar_spectator ? 1 : 0),
104 test_bit(SDF_WITHDRAWN, &f),
105 test_bit(SDF_NOBARRIERS, &f),
106 test_bit(SDF_NORECOVERY, &f),
107 test_bit(SDF_DEMOTE, &f),
108 test_bit(SDF_NOJOURNALID, &f),
109 (sb_rdonly(sdp->sd_vfs) ? 1 : 0),
110 test_bit(SDF_RORECOVERY, &f),
111 test_bit(SDF_SKIP_DLM_UNLOCK, &f),
112 test_bit(SDF_FORCE_AIL_FLUSH, &f),
113 test_bit(SDF_FS_FROZEN, &f),
114 test_bit(SDF_WITHDRAWING, &f),
115 test_bit(SDF_WITHDRAW_IN_PROG, &f),
116 test_bit(SDF_REMOTE_WITHDRAW, &f),
117 test_bit(SDF_WITHDRAW_RECOVERY, &f),
119 rwsem_is_locked(&sdp->sd_log_flush_lock),
120 sdp->sd_log_num_revoke,
121 atomic_read(&sdp->sd_log_in_flight),
122 atomic_read(&sdp->sd_log_blks_needed),
123 atomic_read(&sdp->sd_log_blks_free),
124 sdp->sd_log_flush_head,
125 sdp->sd_log_flush_tail,
126 sdp->sd_log_blks_reserved,
127 atomic_read(&sdp->sd_log_revokes_available));
131 static ssize_t fsname_show(struct gfs2_sbd *sdp, char *buf)
133 return snprintf(buf, PAGE_SIZE, "%s\n", sdp->sd_fsname);
136 static ssize_t uuid_show(struct gfs2_sbd *sdp, char *buf)
138 struct super_block *s = sdp->sd_vfs;
141 if (uuid_is_null(&s->s_uuid))
143 return snprintf(buf, PAGE_SIZE, "%pUB\n", &s->s_uuid);
146 static ssize_t freeze_show(struct gfs2_sbd *sdp, char *buf)
148 struct super_block *sb = sdp->sd_vfs;
149 int frozen = (sb->s_writers.frozen == SB_UNFROZEN) ? 0 : 1;
151 return snprintf(buf, PAGE_SIZE, "%d\n", frozen);
154 static ssize_t freeze_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
158 error = kstrtoint(buf, 0, &n);
162 if (!capable(CAP_SYS_ADMIN))
167 error = thaw_super(sdp->sd_vfs);
170 error = freeze_super(sdp->sd_vfs);
177 fs_warn(sdp, "freeze %d error %d\n", n, error);
184 static ssize_t withdraw_show(struct gfs2_sbd *sdp, char *buf)
186 unsigned int b = gfs2_withdrawn(sdp);
187 return snprintf(buf, PAGE_SIZE, "%u\n", b);
190 static ssize_t withdraw_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
194 if (!capable(CAP_SYS_ADMIN))
197 error = kstrtoint(buf, 0, &val);
204 gfs2_lm(sdp, "withdrawing from cluster at user's request\n");
210 static ssize_t statfs_sync_store(struct gfs2_sbd *sdp, const char *buf,
215 if (!capable(CAP_SYS_ADMIN))
218 error = kstrtoint(buf, 0, &val);
225 gfs2_statfs_sync(sdp->sd_vfs, 0);
229 static ssize_t quota_sync_store(struct gfs2_sbd *sdp, const char *buf,
234 if (!capable(CAP_SYS_ADMIN))
237 error = kstrtoint(buf, 0, &val);
244 gfs2_quota_sync(sdp->sd_vfs, 0);
248 static ssize_t quota_refresh_user_store(struct gfs2_sbd *sdp, const char *buf,
255 if (!capable(CAP_SYS_ADMIN))
258 error = kstrtou32(buf, 0, &id);
262 qid = make_kqid(current_user_ns(), USRQUOTA, id);
266 error = gfs2_quota_refresh(sdp, qid);
267 return error ? error : len;
270 static ssize_t quota_refresh_group_store(struct gfs2_sbd *sdp, const char *buf,
277 if (!capable(CAP_SYS_ADMIN))
280 error = kstrtou32(buf, 0, &id);
284 qid = make_kqid(current_user_ns(), GRPQUOTA, id);
288 error = gfs2_quota_refresh(sdp, qid);
289 return error ? error : len;
292 static ssize_t demote_rq_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
294 struct gfs2_glock *gl;
295 const struct gfs2_glock_operations *glops;
298 unsigned long long glnum;
302 if (!capable(CAP_SYS_ADMIN))
305 rv = sscanf(buf, "%u:%llu %15s", &gltype, &glnum,
310 if (strcmp(mode, "EX") == 0)
311 glmode = LM_ST_UNLOCKED;
312 else if ((strcmp(mode, "CW") == 0) || (strcmp(mode, "DF") == 0))
313 glmode = LM_ST_DEFERRED;
314 else if ((strcmp(mode, "PR") == 0) || (strcmp(mode, "SH") == 0))
315 glmode = LM_ST_SHARED;
319 if (gltype > LM_TYPE_JOURNAL)
321 if (gltype == LM_TYPE_NONDISK && glnum == GFS2_FREEZE_LOCK)
322 glops = &gfs2_freeze_glops;
324 glops = gfs2_glops_list[gltype];
327 if (!test_and_set_bit(SDF_DEMOTE, &sdp->sd_flags))
328 fs_info(sdp, "demote interface used\n");
329 rv = gfs2_glock_get(sdp, glnum, glops, 0, &gl);
332 gfs2_glock_cb(gl, glmode);
338 #define GFS2_ATTR(name, mode, show, store) \
339 static struct gfs2_attr gfs2_attr_##name = __ATTR(name, mode, show, store)
341 GFS2_ATTR(id, 0444, id_show, NULL);
342 GFS2_ATTR(fsname, 0444, fsname_show, NULL);
343 GFS2_ATTR(uuid, 0444, uuid_show, NULL);
344 GFS2_ATTR(freeze, 0644, freeze_show, freeze_store);
345 GFS2_ATTR(withdraw, 0644, withdraw_show, withdraw_store);
346 GFS2_ATTR(statfs_sync, 0200, NULL, statfs_sync_store);
347 GFS2_ATTR(quota_sync, 0200, NULL, quota_sync_store);
348 GFS2_ATTR(quota_refresh_user, 0200, NULL, quota_refresh_user_store);
349 GFS2_ATTR(quota_refresh_group, 0200, NULL, quota_refresh_group_store);
350 GFS2_ATTR(demote_rq, 0200, NULL, demote_rq_store);
351 GFS2_ATTR(status, 0400, status_show, NULL);
353 static struct attribute *gfs2_attrs[] = {
355 &gfs2_attr_fsname.attr,
356 &gfs2_attr_uuid.attr,
357 &gfs2_attr_freeze.attr,
358 &gfs2_attr_withdraw.attr,
359 &gfs2_attr_statfs_sync.attr,
360 &gfs2_attr_quota_sync.attr,
361 &gfs2_attr_quota_refresh_user.attr,
362 &gfs2_attr_quota_refresh_group.attr,
363 &gfs2_attr_demote_rq.attr,
364 &gfs2_attr_status.attr,
367 ATTRIBUTE_GROUPS(gfs2);
369 static void gfs2_sbd_release(struct kobject *kobj)
371 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
373 complete(&sdp->sd_kobj_unregister);
376 static struct kobj_type gfs2_ktype = {
377 .release = gfs2_sbd_release,
378 .default_groups = gfs2_groups,
379 .sysfs_ops = &gfs2_attr_ops,
384 * lock_module. Originally from lock_dlm
387 static ssize_t proto_name_show(struct gfs2_sbd *sdp, char *buf)
389 const struct lm_lockops *ops = sdp->sd_lockstruct.ls_ops;
390 return sprintf(buf, "%s\n", ops->lm_proto_name);
393 static ssize_t block_show(struct gfs2_sbd *sdp, char *buf)
395 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
399 if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))
401 ret = sprintf(buf, "%d\n", val);
405 static ssize_t block_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
407 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
410 ret = kstrtoint(buf, 0, &val);
415 set_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
417 clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
418 smp_mb__after_atomic();
419 gfs2_glock_thaw(sdp);
426 static ssize_t wdack_show(struct gfs2_sbd *sdp, char *buf)
428 int val = completion_done(&sdp->sd_wdack) ? 1 : 0;
430 return sprintf(buf, "%d\n", val);
433 static ssize_t wdack_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
437 ret = kstrtoint(buf, 0, &val);
442 !strcmp(sdp->sd_lockstruct.ls_ops->lm_proto_name, "lock_dlm"))
443 complete(&sdp->sd_wdack);
449 static ssize_t lkfirst_show(struct gfs2_sbd *sdp, char *buf)
451 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
452 return sprintf(buf, "%d\n", ls->ls_first);
455 static ssize_t lkfirst_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
460 rv = sscanf(buf, "%u", &first);
461 if (rv != 1 || first > 1)
463 rv = wait_for_completion_killable(&sdp->sd_locking_init);
466 spin_lock(&sdp->sd_jindex_spin);
468 if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
471 if (sdp->sd_args.ar_spectator)
473 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
475 sdp->sd_lockstruct.ls_first = first;
478 spin_unlock(&sdp->sd_jindex_spin);
479 return rv ? rv : len;
482 static ssize_t first_done_show(struct gfs2_sbd *sdp, char *buf)
484 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
485 return sprintf(buf, "%d\n", !!test_bit(DFL_FIRST_MOUNT_DONE, &ls->ls_recover_flags));
488 int gfs2_recover_set(struct gfs2_sbd *sdp, unsigned jid)
490 struct gfs2_jdesc *jd;
493 /* Wait for our primary journal to be initialized */
494 wait_for_completion(&sdp->sd_journal_ready);
496 spin_lock(&sdp->sd_jindex_spin);
499 * If we're a spectator, we use journal0, but it's not really ours.
500 * So we need to wait for its recovery too. If we skip it we'd never
501 * queue work to the recovery workqueue, and so its completion would
502 * never clear the DFL_BLOCK_LOCKS flag, so all our locks would
503 * permanently stop working.
507 if (sdp->sd_jdesc->jd_jid == jid && !sdp->sd_args.ar_spectator)
510 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
511 if (jd->jd_jid != jid && !sdp->sd_args.ar_spectator)
513 rv = gfs2_recover_journal(jd, false);
517 spin_unlock(&sdp->sd_jindex_spin);
521 static ssize_t recover_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
526 rv = sscanf(buf, "%u", &jid);
530 if (test_bit(SDF_NORECOVERY, &sdp->sd_flags)) {
535 rv = gfs2_recover_set(sdp, jid);
537 return rv ? rv : len;
540 static ssize_t recover_done_show(struct gfs2_sbd *sdp, char *buf)
542 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
543 return sprintf(buf, "%d\n", ls->ls_recover_jid_done);
546 static ssize_t recover_status_show(struct gfs2_sbd *sdp, char *buf)
548 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
549 return sprintf(buf, "%d\n", ls->ls_recover_jid_status);
552 static ssize_t jid_show(struct gfs2_sbd *sdp, char *buf)
554 return sprintf(buf, "%d\n", sdp->sd_lockstruct.ls_jid);
557 static ssize_t jid_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
562 rv = sscanf(buf, "%d", &jid);
565 rv = wait_for_completion_killable(&sdp->sd_locking_init);
568 spin_lock(&sdp->sd_jindex_spin);
570 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
573 if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
576 if (sdp->sd_args.ar_spectator && jid > 0)
578 sdp->sd_lockstruct.ls_jid = jid;
579 clear_bit(SDF_NOJOURNALID, &sdp->sd_flags);
580 smp_mb__after_atomic();
581 wake_up_bit(&sdp->sd_flags, SDF_NOJOURNALID);
583 spin_unlock(&sdp->sd_jindex_spin);
584 return rv ? rv : len;
587 #define GDLM_ATTR(_name,_mode,_show,_store) \
588 static struct gfs2_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
590 GDLM_ATTR(proto_name, 0444, proto_name_show, NULL);
591 GDLM_ATTR(block, 0644, block_show, block_store);
592 GDLM_ATTR(withdraw, 0644, wdack_show, wdack_store);
593 GDLM_ATTR(jid, 0644, jid_show, jid_store);
594 GDLM_ATTR(first, 0644, lkfirst_show, lkfirst_store);
595 GDLM_ATTR(first_done, 0444, first_done_show, NULL);
596 GDLM_ATTR(recover, 0600, NULL, recover_store);
597 GDLM_ATTR(recover_done, 0444, recover_done_show, NULL);
598 GDLM_ATTR(recover_status, 0444, recover_status_show, NULL);
600 static struct attribute *lock_module_attrs[] = {
601 &gdlm_attr_proto_name.attr,
602 &gdlm_attr_block.attr,
603 &gdlm_attr_withdraw.attr,
605 &gdlm_attr_first.attr,
606 &gdlm_attr_first_done.attr,
607 &gdlm_attr_recover.attr,
608 &gdlm_attr_recover_done.attr,
609 &gdlm_attr_recover_status.attr,
614 * get and set struct gfs2_tune fields
617 static ssize_t quota_scale_show(struct gfs2_sbd *sdp, char *buf)
619 return snprintf(buf, PAGE_SIZE, "%u %u\n",
620 sdp->sd_tune.gt_quota_scale_num,
621 sdp->sd_tune.gt_quota_scale_den);
624 static ssize_t quota_scale_store(struct gfs2_sbd *sdp, const char *buf,
627 struct gfs2_tune *gt = &sdp->sd_tune;
630 if (!capable(CAP_SYS_ADMIN))
633 if (sscanf(buf, "%u %u", &x, &y) != 2 || !y)
636 spin_lock(>->gt_spin);
637 gt->gt_quota_scale_num = x;
638 gt->gt_quota_scale_den = y;
639 spin_unlock(>->gt_spin);
643 static ssize_t tune_set(struct gfs2_sbd *sdp, unsigned int *field,
644 int check_zero, const char *buf, size_t len)
646 struct gfs2_tune *gt = &sdp->sd_tune;
650 if (!capable(CAP_SYS_ADMIN))
653 error = kstrtouint(buf, 0, &x);
657 if (check_zero && !x)
660 spin_lock(>->gt_spin);
662 spin_unlock(>->gt_spin);
666 #define TUNE_ATTR_3(name, show, store) \
667 static struct gfs2_attr tune_attr_##name = __ATTR(name, 0644, show, store)
669 #define TUNE_ATTR_2(name, store) \
670 static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf) \
672 return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name); \
674 TUNE_ATTR_3(name, name##_show, store)
676 #define TUNE_ATTR(name, check_zero) \
677 static ssize_t name##_store(struct gfs2_sbd *sdp, const char *buf, size_t len)\
679 return tune_set(sdp, &sdp->sd_tune.gt_##name, check_zero, buf, len); \
681 TUNE_ATTR_2(name, name##_store)
683 TUNE_ATTR(quota_warn_period, 0);
684 TUNE_ATTR(quota_quantum, 0);
685 TUNE_ATTR(max_readahead, 0);
686 TUNE_ATTR(complain_secs, 0);
687 TUNE_ATTR(statfs_slow, 0);
688 TUNE_ATTR(new_files_jdata, 0);
689 TUNE_ATTR(statfs_quantum, 1);
690 TUNE_ATTR_3(quota_scale, quota_scale_show, quota_scale_store);
692 static struct attribute *tune_attrs[] = {
693 &tune_attr_quota_warn_period.attr,
694 &tune_attr_quota_quantum.attr,
695 &tune_attr_max_readahead.attr,
696 &tune_attr_complain_secs.attr,
697 &tune_attr_statfs_slow.attr,
698 &tune_attr_statfs_quantum.attr,
699 &tune_attr_quota_scale.attr,
700 &tune_attr_new_files_jdata.attr,
704 static const struct attribute_group tune_group = {
709 static const struct attribute_group lock_module_group = {
710 .name = "lock_module",
711 .attrs = lock_module_attrs,
714 int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
716 struct super_block *sb = sdp->sd_vfs;
720 char *envp[] = { ro, spectator, NULL };
722 sprintf(ro, "RDONLY=%d", sb_rdonly(sb));
723 sprintf(spectator, "SPECTATOR=%d", sdp->sd_args.ar_spectator ? 1 : 0);
725 init_completion(&sdp->sd_kobj_unregister);
726 sdp->sd_kobj.kset = gfs2_kset;
727 error = kobject_init_and_add(&sdp->sd_kobj, &gfs2_ktype, NULL,
728 "%s", sdp->sd_table_name);
732 error = sysfs_create_group(&sdp->sd_kobj, &tune_group);
736 error = sysfs_create_group(&sdp->sd_kobj, &lock_module_group);
740 error = sysfs_create_link(&sdp->sd_kobj,
741 &disk_to_dev(sb->s_bdev->bd_disk)->kobj,
744 goto fail_lock_module;
746 kobject_uevent_env(&sdp->sd_kobj, KOBJ_ADD, envp);
750 sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
752 sysfs_remove_group(&sdp->sd_kobj, &tune_group);
754 fs_err(sdp, "error %d adding sysfs files\n", error);
755 kobject_put(&sdp->sd_kobj);
756 wait_for_completion(&sdp->sd_kobj_unregister);
757 sb->s_fs_info = NULL;
761 void gfs2_sys_fs_del(struct gfs2_sbd *sdp)
763 sysfs_remove_link(&sdp->sd_kobj, "device");
764 sysfs_remove_group(&sdp->sd_kobj, &tune_group);
765 sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
766 kobject_put(&sdp->sd_kobj);
767 wait_for_completion(&sdp->sd_kobj_unregister);
770 static int gfs2_uevent(struct kobject *kobj, struct kobj_uevent_env *env)
772 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
773 struct super_block *s = sdp->sd_vfs;
775 add_uevent_var(env, "LOCKTABLE=%s", sdp->sd_table_name);
776 add_uevent_var(env, "LOCKPROTO=%s", sdp->sd_proto_name);
777 if (!test_bit(SDF_NOJOURNALID, &sdp->sd_flags))
778 add_uevent_var(env, "JOURNALID=%d", sdp->sd_lockstruct.ls_jid);
779 if (!uuid_is_null(&s->s_uuid))
780 add_uevent_var(env, "UUID=%pUB", &s->s_uuid);
784 static const struct kset_uevent_ops gfs2_uevent_ops = {
785 .uevent = gfs2_uevent,
788 int gfs2_sys_init(void)
790 gfs2_kset = kset_create_and_add("gfs2", &gfs2_uevent_ops, fs_kobj);
796 void gfs2_sys_uninit(void)
798 kset_unregister(gfs2_kset);