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/genhd.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
fsname_show(struct gfs2_sbd
*sdp
, char *buf
)
68 return snprintf(buf
, PAGE_SIZE
, "%s\n", sdp
->sd_fsname
);
71 static ssize_t
uuid_show(struct gfs2_sbd
*sdp
, char *buf
)
73 struct super_block
*s
= sdp
->sd_vfs
;
76 if (uuid_is_null(&s
->s_uuid
))
78 return snprintf(buf
, PAGE_SIZE
, "%pUB\n", &s
->s_uuid
);
81 static ssize_t
freeze_show(struct gfs2_sbd
*sdp
, char *buf
)
83 struct super_block
*sb
= sdp
->sd_vfs
;
84 int frozen
= (sb
->s_writers
.frozen
== SB_UNFROZEN
) ? 0 : 1;
86 return snprintf(buf
, PAGE_SIZE
, "%d\n", frozen
);
89 static ssize_t
freeze_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
93 error
= kstrtoint(buf
, 0, &n
);
97 if (!capable(CAP_SYS_ADMIN
))
102 error
= thaw_super(sdp
->sd_vfs
);
105 error
= freeze_super(sdp
->sd_vfs
);
112 fs_warn(sdp
, "freeze %d error %d\n", n
, error
);
119 static ssize_t
withdraw_show(struct gfs2_sbd
*sdp
, char *buf
)
121 unsigned int b
= gfs2_withdrawn(sdp
);
122 return snprintf(buf
, PAGE_SIZE
, "%u\n", b
);
125 static ssize_t
withdraw_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
129 if (!capable(CAP_SYS_ADMIN
))
132 error
= kstrtoint(buf
, 0, &val
);
139 gfs2_lm(sdp
, "withdrawing from cluster at user's request\n");
145 static ssize_t
statfs_sync_store(struct gfs2_sbd
*sdp
, const char *buf
,
150 if (!capable(CAP_SYS_ADMIN
))
153 error
= kstrtoint(buf
, 0, &val
);
160 gfs2_statfs_sync(sdp
->sd_vfs
, 0);
164 static ssize_t
quota_sync_store(struct gfs2_sbd
*sdp
, const char *buf
,
169 if (!capable(CAP_SYS_ADMIN
))
172 error
= kstrtoint(buf
, 0, &val
);
179 gfs2_quota_sync(sdp
->sd_vfs
, 0);
183 static ssize_t
quota_refresh_user_store(struct gfs2_sbd
*sdp
, const char *buf
,
190 if (!capable(CAP_SYS_ADMIN
))
193 error
= kstrtou32(buf
, 0, &id
);
197 qid
= make_kqid(current_user_ns(), USRQUOTA
, id
);
201 error
= gfs2_quota_refresh(sdp
, qid
);
202 return error
? error
: len
;
205 static ssize_t
quota_refresh_group_store(struct gfs2_sbd
*sdp
, const char *buf
,
212 if (!capable(CAP_SYS_ADMIN
))
215 error
= kstrtou32(buf
, 0, &id
);
219 qid
= make_kqid(current_user_ns(), GRPQUOTA
, id
);
223 error
= gfs2_quota_refresh(sdp
, qid
);
224 return error
? error
: len
;
227 static ssize_t
demote_rq_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
229 struct gfs2_glock
*gl
;
230 const struct gfs2_glock_operations
*glops
;
233 unsigned long long glnum
;
237 if (!capable(CAP_SYS_ADMIN
))
240 rv
= sscanf(buf
, "%u:%llu %15s", &gltype
, &glnum
,
245 if (strcmp(mode
, "EX") == 0)
246 glmode
= LM_ST_UNLOCKED
;
247 else if ((strcmp(mode
, "CW") == 0) || (strcmp(mode
, "DF") == 0))
248 glmode
= LM_ST_DEFERRED
;
249 else if ((strcmp(mode
, "PR") == 0) || (strcmp(mode
, "SH") == 0))
250 glmode
= LM_ST_SHARED
;
254 if (gltype
> LM_TYPE_JOURNAL
)
256 if (gltype
== LM_TYPE_NONDISK
&& glnum
== GFS2_FREEZE_LOCK
)
257 glops
= &gfs2_freeze_glops
;
259 glops
= gfs2_glops_list
[gltype
];
262 if (!test_and_set_bit(SDF_DEMOTE
, &sdp
->sd_flags
))
263 fs_info(sdp
, "demote interface used\n");
264 rv
= gfs2_glock_get(sdp
, glnum
, glops
, 0, &gl
);
267 gfs2_glock_cb(gl
, glmode
);
273 #define GFS2_ATTR(name, mode, show, store) \
274 static struct gfs2_attr gfs2_attr_##name = __ATTR(name, mode, show, store)
276 GFS2_ATTR(id
, 0444, id_show
, NULL
);
277 GFS2_ATTR(fsname
, 0444, fsname_show
, NULL
);
278 GFS2_ATTR(uuid
, 0444, uuid_show
, NULL
);
279 GFS2_ATTR(freeze
, 0644, freeze_show
, freeze_store
);
280 GFS2_ATTR(withdraw
, 0644, withdraw_show
, withdraw_store
);
281 GFS2_ATTR(statfs_sync
, 0200, NULL
, statfs_sync_store
);
282 GFS2_ATTR(quota_sync
, 0200, NULL
, quota_sync_store
);
283 GFS2_ATTR(quota_refresh_user
, 0200, NULL
, quota_refresh_user_store
);
284 GFS2_ATTR(quota_refresh_group
, 0200, NULL
, quota_refresh_group_store
);
285 GFS2_ATTR(demote_rq
, 0200, NULL
, demote_rq_store
);
287 static struct attribute
*gfs2_attrs
[] = {
289 &gfs2_attr_fsname
.attr
,
290 &gfs2_attr_uuid
.attr
,
291 &gfs2_attr_freeze
.attr
,
292 &gfs2_attr_withdraw
.attr
,
293 &gfs2_attr_statfs_sync
.attr
,
294 &gfs2_attr_quota_sync
.attr
,
295 &gfs2_attr_quota_refresh_user
.attr
,
296 &gfs2_attr_quota_refresh_group
.attr
,
297 &gfs2_attr_demote_rq
.attr
,
300 ATTRIBUTE_GROUPS(gfs2
);
302 static void gfs2_sbd_release(struct kobject
*kobj
)
304 struct gfs2_sbd
*sdp
= container_of(kobj
, struct gfs2_sbd
, sd_kobj
);
309 static struct kobj_type gfs2_ktype
= {
310 .release
= gfs2_sbd_release
,
311 .default_groups
= gfs2_groups
,
312 .sysfs_ops
= &gfs2_attr_ops
,
317 * lock_module. Originally from lock_dlm
320 static ssize_t
proto_name_show(struct gfs2_sbd
*sdp
, char *buf
)
322 const struct lm_lockops
*ops
= sdp
->sd_lockstruct
.ls_ops
;
323 return sprintf(buf
, "%s\n", ops
->lm_proto_name
);
326 static ssize_t
block_show(struct gfs2_sbd
*sdp
, char *buf
)
328 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
332 if (test_bit(DFL_BLOCK_LOCKS
, &ls
->ls_recover_flags
))
334 ret
= sprintf(buf
, "%d\n", val
);
338 static ssize_t
block_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
340 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
343 ret
= kstrtoint(buf
, 0, &val
);
348 set_bit(DFL_BLOCK_LOCKS
, &ls
->ls_recover_flags
);
350 clear_bit(DFL_BLOCK_LOCKS
, &ls
->ls_recover_flags
);
351 smp_mb__after_atomic();
352 gfs2_glock_thaw(sdp
);
359 static ssize_t
wdack_show(struct gfs2_sbd
*sdp
, char *buf
)
361 int val
= completion_done(&sdp
->sd_wdack
) ? 1 : 0;
363 return sprintf(buf
, "%d\n", val
);
366 static ssize_t
wdack_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
370 ret
= kstrtoint(buf
, 0, &val
);
375 !strcmp(sdp
->sd_lockstruct
.ls_ops
->lm_proto_name
, "lock_dlm"))
376 complete(&sdp
->sd_wdack
);
382 static ssize_t
lkfirst_show(struct gfs2_sbd
*sdp
, char *buf
)
384 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
385 return sprintf(buf
, "%d\n", ls
->ls_first
);
388 static ssize_t
lkfirst_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
393 rv
= sscanf(buf
, "%u", &first
);
394 if (rv
!= 1 || first
> 1)
396 rv
= wait_for_completion_killable(&sdp
->sd_locking_init
);
399 spin_lock(&sdp
->sd_jindex_spin
);
401 if (test_bit(SDF_NOJOURNALID
, &sdp
->sd_flags
) == 0)
404 if (sdp
->sd_args
.ar_spectator
)
406 if (sdp
->sd_lockstruct
.ls_ops
->lm_mount
== NULL
)
408 sdp
->sd_lockstruct
.ls_first
= first
;
411 spin_unlock(&sdp
->sd_jindex_spin
);
412 return rv
? rv
: len
;
415 static ssize_t
first_done_show(struct gfs2_sbd
*sdp
, char *buf
)
417 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
418 return sprintf(buf
, "%d\n", !!test_bit(DFL_FIRST_MOUNT_DONE
, &ls
->ls_recover_flags
));
421 int gfs2_recover_set(struct gfs2_sbd
*sdp
, unsigned jid
)
423 struct gfs2_jdesc
*jd
;
426 /* Wait for our primary journal to be initialized */
427 wait_for_completion(&sdp
->sd_journal_ready
);
429 spin_lock(&sdp
->sd_jindex_spin
);
432 * If we're a spectator, we use journal0, but it's not really ours.
433 * So we need to wait for its recovery too. If we skip it we'd never
434 * queue work to the recovery workqueue, and so its completion would
435 * never clear the DFL_BLOCK_LOCKS flag, so all our locks would
436 * permanently stop working.
440 if (sdp
->sd_jdesc
->jd_jid
== jid
&& !sdp
->sd_args
.ar_spectator
)
443 list_for_each_entry(jd
, &sdp
->sd_jindex_list
, jd_list
) {
444 if (jd
->jd_jid
!= jid
&& !sdp
->sd_args
.ar_spectator
)
446 rv
= gfs2_recover_journal(jd
, false);
450 spin_unlock(&sdp
->sd_jindex_spin
);
454 static ssize_t
recover_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
459 rv
= sscanf(buf
, "%u", &jid
);
463 if (test_bit(SDF_NORECOVERY
, &sdp
->sd_flags
)) {
468 rv
= gfs2_recover_set(sdp
, jid
);
470 return rv
? rv
: len
;
473 static ssize_t
recover_done_show(struct gfs2_sbd
*sdp
, char *buf
)
475 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
476 return sprintf(buf
, "%d\n", ls
->ls_recover_jid_done
);
479 static ssize_t
recover_status_show(struct gfs2_sbd
*sdp
, char *buf
)
481 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
482 return sprintf(buf
, "%d\n", ls
->ls_recover_jid_status
);
485 static ssize_t
jid_show(struct gfs2_sbd
*sdp
, char *buf
)
487 return sprintf(buf
, "%d\n", sdp
->sd_lockstruct
.ls_jid
);
490 static ssize_t
jid_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
495 rv
= sscanf(buf
, "%d", &jid
);
498 rv
= wait_for_completion_killable(&sdp
->sd_locking_init
);
501 spin_lock(&sdp
->sd_jindex_spin
);
503 if (sdp
->sd_lockstruct
.ls_ops
->lm_mount
== NULL
)
506 if (test_bit(SDF_NOJOURNALID
, &sdp
->sd_flags
) == 0)
509 if (sdp
->sd_args
.ar_spectator
&& jid
> 0)
511 sdp
->sd_lockstruct
.ls_jid
= jid
;
512 clear_bit(SDF_NOJOURNALID
, &sdp
->sd_flags
);
513 smp_mb__after_atomic();
514 wake_up_bit(&sdp
->sd_flags
, SDF_NOJOURNALID
);
516 spin_unlock(&sdp
->sd_jindex_spin
);
517 return rv
? rv
: len
;
520 #define GDLM_ATTR(_name,_mode,_show,_store) \
521 static struct gfs2_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
523 GDLM_ATTR(proto_name
, 0444, proto_name_show
, NULL
);
524 GDLM_ATTR(block
, 0644, block_show
, block_store
);
525 GDLM_ATTR(withdraw
, 0644, wdack_show
, wdack_store
);
526 GDLM_ATTR(jid
, 0644, jid_show
, jid_store
);
527 GDLM_ATTR(first
, 0644, lkfirst_show
, lkfirst_store
);
528 GDLM_ATTR(first_done
, 0444, first_done_show
, NULL
);
529 GDLM_ATTR(recover
, 0600, NULL
, recover_store
);
530 GDLM_ATTR(recover_done
, 0444, recover_done_show
, NULL
);
531 GDLM_ATTR(recover_status
, 0444, recover_status_show
, NULL
);
533 static struct attribute
*lock_module_attrs
[] = {
534 &gdlm_attr_proto_name
.attr
,
535 &gdlm_attr_block
.attr
,
536 &gdlm_attr_withdraw
.attr
,
538 &gdlm_attr_first
.attr
,
539 &gdlm_attr_first_done
.attr
,
540 &gdlm_attr_recover
.attr
,
541 &gdlm_attr_recover_done
.attr
,
542 &gdlm_attr_recover_status
.attr
,
547 * get and set struct gfs2_tune fields
550 static ssize_t
quota_scale_show(struct gfs2_sbd
*sdp
, char *buf
)
552 return snprintf(buf
, PAGE_SIZE
, "%u %u\n",
553 sdp
->sd_tune
.gt_quota_scale_num
,
554 sdp
->sd_tune
.gt_quota_scale_den
);
557 static ssize_t
quota_scale_store(struct gfs2_sbd
*sdp
, const char *buf
,
560 struct gfs2_tune
*gt
= &sdp
->sd_tune
;
563 if (!capable(CAP_SYS_ADMIN
))
566 if (sscanf(buf
, "%u %u", &x
, &y
) != 2 || !y
)
569 spin_lock(>
->gt_spin
);
570 gt
->gt_quota_scale_num
= x
;
571 gt
->gt_quota_scale_den
= y
;
572 spin_unlock(>
->gt_spin
);
576 static ssize_t
tune_set(struct gfs2_sbd
*sdp
, unsigned int *field
,
577 int check_zero
, const char *buf
, size_t len
)
579 struct gfs2_tune
*gt
= &sdp
->sd_tune
;
583 if (!capable(CAP_SYS_ADMIN
))
586 error
= kstrtouint(buf
, 0, &x
);
590 if (check_zero
&& !x
)
593 spin_lock(>
->gt_spin
);
595 spin_unlock(>
->gt_spin
);
599 #define TUNE_ATTR_3(name, show, store) \
600 static struct gfs2_attr tune_attr_##name = __ATTR(name, 0644, show, store)
602 #define TUNE_ATTR_2(name, store) \
603 static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf) \
605 return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name); \
607 TUNE_ATTR_3(name, name##_show, store)
609 #define TUNE_ATTR(name, check_zero) \
610 static ssize_t name##_store(struct gfs2_sbd *sdp, const char *buf, size_t len)\
612 return tune_set(sdp, &sdp->sd_tune.gt_##name, check_zero, buf, len); \
614 TUNE_ATTR_2(name, name##_store)
616 TUNE_ATTR(quota_warn_period
, 0);
617 TUNE_ATTR(quota_quantum
, 0);
618 TUNE_ATTR(max_readahead
, 0);
619 TUNE_ATTR(complain_secs
, 0);
620 TUNE_ATTR(statfs_slow
, 0);
621 TUNE_ATTR(new_files_jdata
, 0);
622 TUNE_ATTR(statfs_quantum
, 1);
623 TUNE_ATTR_3(quota_scale
, quota_scale_show
, quota_scale_store
);
625 static struct attribute
*tune_attrs
[] = {
626 &tune_attr_quota_warn_period
.attr
,
627 &tune_attr_quota_quantum
.attr
,
628 &tune_attr_max_readahead
.attr
,
629 &tune_attr_complain_secs
.attr
,
630 &tune_attr_statfs_slow
.attr
,
631 &tune_attr_statfs_quantum
.attr
,
632 &tune_attr_quota_scale
.attr
,
633 &tune_attr_new_files_jdata
.attr
,
637 static const struct attribute_group tune_group
= {
642 static const struct attribute_group lock_module_group
= {
643 .name
= "lock_module",
644 .attrs
= lock_module_attrs
,
647 int gfs2_sys_fs_add(struct gfs2_sbd
*sdp
)
649 struct super_block
*sb
= sdp
->sd_vfs
;
653 char *envp
[] = { ro
, spectator
, NULL
};
655 sprintf(ro
, "RDONLY=%d", sb_rdonly(sb
));
656 sprintf(spectator
, "SPECTATOR=%d", sdp
->sd_args
.ar_spectator
? 1 : 0);
658 sdp
->sd_kobj
.kset
= gfs2_kset
;
659 error
= kobject_init_and_add(&sdp
->sd_kobj
, &gfs2_ktype
, NULL
,
660 "%s", sdp
->sd_table_name
);
664 error
= sysfs_create_group(&sdp
->sd_kobj
, &tune_group
);
668 error
= sysfs_create_group(&sdp
->sd_kobj
, &lock_module_group
);
672 error
= sysfs_create_link(&sdp
->sd_kobj
,
673 &disk_to_dev(sb
->s_bdev
->bd_disk
)->kobj
,
676 goto fail_lock_module
;
678 kobject_uevent_env(&sdp
->sd_kobj
, KOBJ_ADD
, envp
);
682 sysfs_remove_group(&sdp
->sd_kobj
, &lock_module_group
);
684 sysfs_remove_group(&sdp
->sd_kobj
, &tune_group
);
686 fs_err(sdp
, "error %d adding sysfs files\n", error
);
687 kobject_put(&sdp
->sd_kobj
);
688 sb
->s_fs_info
= NULL
;
692 void gfs2_sys_fs_del(struct gfs2_sbd
*sdp
)
694 sysfs_remove_link(&sdp
->sd_kobj
, "device");
695 sysfs_remove_group(&sdp
->sd_kobj
, &tune_group
);
696 sysfs_remove_group(&sdp
->sd_kobj
, &lock_module_group
);
697 kobject_put(&sdp
->sd_kobj
);
700 static int gfs2_uevent(struct kset
*kset
, struct kobject
*kobj
,
701 struct kobj_uevent_env
*env
)
703 struct gfs2_sbd
*sdp
= container_of(kobj
, struct gfs2_sbd
, sd_kobj
);
704 struct super_block
*s
= sdp
->sd_vfs
;
706 add_uevent_var(env
, "LOCKTABLE=%s", sdp
->sd_table_name
);
707 add_uevent_var(env
, "LOCKPROTO=%s", sdp
->sd_proto_name
);
708 if (!test_bit(SDF_NOJOURNALID
, &sdp
->sd_flags
))
709 add_uevent_var(env
, "JOURNALID=%d", sdp
->sd_lockstruct
.ls_jid
);
710 if (!uuid_is_null(&s
->s_uuid
))
711 add_uevent_var(env
, "UUID=%pUB", &s
->s_uuid
);
715 static const struct kset_uevent_ops gfs2_uevent_ops
= {
716 .uevent
= gfs2_uevent
,
719 int gfs2_sys_init(void)
721 gfs2_kset
= kset_create_and_add("gfs2", &gfs2_uevent_ops
, fs_kobj
);
727 void gfs2_sys_uninit(void)
729 kset_unregister(gfs2_kset
);