2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/sched.h>
13 #include <linux/spinlock.h>
14 #include <linux/completion.h>
15 #include <linux/buffer_head.h>
16 #include <linux/module.h>
17 #include <linux/kobject.h>
18 #include <asm/uaccess.h>
19 #include <linux/gfs2_ondisk.h>
20 #include <linux/genhd.h>
33 struct attribute attr
;
34 ssize_t (*show
)(struct gfs2_sbd
*, char *);
35 ssize_t (*store
)(struct gfs2_sbd
*, const char *, size_t);
38 static ssize_t
gfs2_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
41 struct gfs2_sbd
*sdp
= container_of(kobj
, struct gfs2_sbd
, sd_kobj
);
42 struct gfs2_attr
*a
= container_of(attr
, struct gfs2_attr
, attr
);
43 return a
->show
? a
->show(sdp
, buf
) : 0;
46 static ssize_t
gfs2_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
47 const char *buf
, size_t len
)
49 struct gfs2_sbd
*sdp
= container_of(kobj
, struct gfs2_sbd
, sd_kobj
);
50 struct gfs2_attr
*a
= container_of(attr
, struct gfs2_attr
, attr
);
51 return a
->store
? a
->store(sdp
, buf
, len
) : len
;
54 static const struct sysfs_ops gfs2_attr_ops
= {
55 .show
= gfs2_attr_show
,
56 .store
= gfs2_attr_store
,
60 static struct kset
*gfs2_kset
;
62 static ssize_t
id_show(struct gfs2_sbd
*sdp
, char *buf
)
64 return snprintf(buf
, PAGE_SIZE
, "%u:%u\n",
65 MAJOR(sdp
->sd_vfs
->s_dev
), MINOR(sdp
->sd_vfs
->s_dev
));
68 static ssize_t
fsname_show(struct gfs2_sbd
*sdp
, char *buf
)
70 return snprintf(buf
, PAGE_SIZE
, "%s\n", sdp
->sd_fsname
);
73 static int gfs2_uuid_valid(const u8
*uuid
)
77 for (i
= 0; i
< 16; i
++) {
84 static ssize_t
uuid_show(struct gfs2_sbd
*sdp
, char *buf
)
86 struct super_block
*s
= sdp
->sd_vfs
;
87 const u8
*uuid
= s
->s_uuid
;
89 if (!gfs2_uuid_valid(uuid
))
91 return snprintf(buf
, PAGE_SIZE
, "%pUB\n", uuid
);
94 static ssize_t
freeze_show(struct gfs2_sbd
*sdp
, char *buf
)
96 struct super_block
*sb
= sdp
->sd_vfs
;
97 int frozen
= (sb
->s_writers
.frozen
== SB_UNFROZEN
) ? 0 : 1;
99 return snprintf(buf
, PAGE_SIZE
, "%d\n", frozen
);
102 static ssize_t
freeze_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
106 error
= kstrtoint(buf
, 0, &n
);
110 if (!capable(CAP_SYS_ADMIN
))
115 error
= thaw_super(sdp
->sd_vfs
);
118 error
= freeze_super(sdp
->sd_vfs
);
125 fs_warn(sdp
, "freeze %d error %d", n
, error
);
132 static ssize_t
withdraw_show(struct gfs2_sbd
*sdp
, char *buf
)
134 unsigned int b
= test_bit(SDF_SHUTDOWN
, &sdp
->sd_flags
);
135 return snprintf(buf
, PAGE_SIZE
, "%u\n", b
);
138 static ssize_t
withdraw_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
142 if (!capable(CAP_SYS_ADMIN
))
145 error
= kstrtoint(buf
, 0, &val
);
152 gfs2_lm_withdraw(sdp
, "withdrawing from cluster at user's request\n");
157 static ssize_t
statfs_sync_store(struct gfs2_sbd
*sdp
, const char *buf
,
162 if (!capable(CAP_SYS_ADMIN
))
165 error
= kstrtoint(buf
, 0, &val
);
172 gfs2_statfs_sync(sdp
->sd_vfs
, 0);
176 static ssize_t
quota_sync_store(struct gfs2_sbd
*sdp
, const char *buf
,
181 if (!capable(CAP_SYS_ADMIN
))
184 error
= kstrtoint(buf
, 0, &val
);
191 gfs2_quota_sync(sdp
->sd_vfs
, 0);
195 static ssize_t
quota_refresh_user_store(struct gfs2_sbd
*sdp
, const char *buf
,
202 if (!capable(CAP_SYS_ADMIN
))
205 error
= kstrtou32(buf
, 0, &id
);
209 qid
= make_kqid(current_user_ns(), USRQUOTA
, id
);
213 error
= gfs2_quota_refresh(sdp
, qid
);
214 return error
? error
: len
;
217 static ssize_t
quota_refresh_group_store(struct gfs2_sbd
*sdp
, const char *buf
,
224 if (!capable(CAP_SYS_ADMIN
))
227 error
= kstrtou32(buf
, 0, &id
);
231 qid
= make_kqid(current_user_ns(), GRPQUOTA
, id
);
235 error
= gfs2_quota_refresh(sdp
, qid
);
236 return error
? error
: len
;
239 static ssize_t
demote_rq_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
241 struct gfs2_glock
*gl
;
242 const struct gfs2_glock_operations
*glops
;
245 unsigned long long glnum
;
249 if (!capable(CAP_SYS_ADMIN
))
252 rv
= sscanf(buf
, "%u:%llu %15s", &gltype
, &glnum
,
257 if (strcmp(mode
, "EX") == 0)
258 glmode
= LM_ST_UNLOCKED
;
259 else if ((strcmp(mode
, "CW") == 0) || (strcmp(mode
, "DF") == 0))
260 glmode
= LM_ST_DEFERRED
;
261 else if ((strcmp(mode
, "PR") == 0) || (strcmp(mode
, "SH") == 0))
262 glmode
= LM_ST_SHARED
;
266 if (gltype
> LM_TYPE_JOURNAL
)
268 if (gltype
== LM_TYPE_NONDISK
&& glnum
== GFS2_FREEZE_LOCK
)
269 glops
= &gfs2_freeze_glops
;
271 glops
= gfs2_glops_list
[gltype
];
274 if (!test_and_set_bit(SDF_DEMOTE
, &sdp
->sd_flags
))
275 fs_info(sdp
, "demote interface used\n");
276 rv
= gfs2_glock_get(sdp
, glnum
, glops
, 0, &gl
);
279 gfs2_glock_cb(gl
, glmode
);
285 #define GFS2_ATTR(name, mode, show, store) \
286 static struct gfs2_attr gfs2_attr_##name = __ATTR(name, mode, show, store)
288 GFS2_ATTR(id
, 0444, id_show
, NULL
);
289 GFS2_ATTR(fsname
, 0444, fsname_show
, NULL
);
290 GFS2_ATTR(uuid
, 0444, uuid_show
, NULL
);
291 GFS2_ATTR(freeze
, 0644, freeze_show
, freeze_store
);
292 GFS2_ATTR(withdraw
, 0644, withdraw_show
, withdraw_store
);
293 GFS2_ATTR(statfs_sync
, 0200, NULL
, statfs_sync_store
);
294 GFS2_ATTR(quota_sync
, 0200, NULL
, quota_sync_store
);
295 GFS2_ATTR(quota_refresh_user
, 0200, NULL
, quota_refresh_user_store
);
296 GFS2_ATTR(quota_refresh_group
, 0200, NULL
, quota_refresh_group_store
);
297 GFS2_ATTR(demote_rq
, 0200, NULL
, demote_rq_store
);
299 static struct attribute
*gfs2_attrs
[] = {
301 &gfs2_attr_fsname
.attr
,
302 &gfs2_attr_uuid
.attr
,
303 &gfs2_attr_freeze
.attr
,
304 &gfs2_attr_withdraw
.attr
,
305 &gfs2_attr_statfs_sync
.attr
,
306 &gfs2_attr_quota_sync
.attr
,
307 &gfs2_attr_quota_refresh_user
.attr
,
308 &gfs2_attr_quota_refresh_group
.attr
,
309 &gfs2_attr_demote_rq
.attr
,
313 static void gfs2_sbd_release(struct kobject
*kobj
)
315 struct gfs2_sbd
*sdp
= container_of(kobj
, struct gfs2_sbd
, sd_kobj
);
320 static struct kobj_type gfs2_ktype
= {
321 .release
= gfs2_sbd_release
,
322 .default_attrs
= gfs2_attrs
,
323 .sysfs_ops
= &gfs2_attr_ops
,
328 * lock_module. Originally from lock_dlm
331 static ssize_t
proto_name_show(struct gfs2_sbd
*sdp
, char *buf
)
333 const struct lm_lockops
*ops
= sdp
->sd_lockstruct
.ls_ops
;
334 return sprintf(buf
, "%s\n", ops
->lm_proto_name
);
337 static ssize_t
block_show(struct gfs2_sbd
*sdp
, char *buf
)
339 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
343 if (test_bit(DFL_BLOCK_LOCKS
, &ls
->ls_recover_flags
))
345 ret
= sprintf(buf
, "%d\n", val
);
349 static ssize_t
block_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
351 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
354 ret
= kstrtoint(buf
, 0, &val
);
359 set_bit(DFL_BLOCK_LOCKS
, &ls
->ls_recover_flags
);
361 clear_bit(DFL_BLOCK_LOCKS
, &ls
->ls_recover_flags
);
362 smp_mb__after_atomic();
363 gfs2_glock_thaw(sdp
);
370 static ssize_t
wdack_show(struct gfs2_sbd
*sdp
, char *buf
)
372 int val
= completion_done(&sdp
->sd_wdack
) ? 1 : 0;
374 return sprintf(buf
, "%d\n", val
);
377 static ssize_t
wdack_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
381 ret
= kstrtoint(buf
, 0, &val
);
386 !strcmp(sdp
->sd_lockstruct
.ls_ops
->lm_proto_name
, "lock_dlm"))
387 complete(&sdp
->sd_wdack
);
393 static ssize_t
lkfirst_show(struct gfs2_sbd
*sdp
, char *buf
)
395 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
396 return sprintf(buf
, "%d\n", ls
->ls_first
);
399 static ssize_t
lkfirst_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
404 rv
= sscanf(buf
, "%u", &first
);
405 if (rv
!= 1 || first
> 1)
407 rv
= wait_for_completion_killable(&sdp
->sd_locking_init
);
410 spin_lock(&sdp
->sd_jindex_spin
);
412 if (test_bit(SDF_NOJOURNALID
, &sdp
->sd_flags
) == 0)
415 if (sdp
->sd_args
.ar_spectator
)
417 if (sdp
->sd_lockstruct
.ls_ops
->lm_mount
== NULL
)
419 sdp
->sd_lockstruct
.ls_first
= first
;
422 spin_unlock(&sdp
->sd_jindex_spin
);
423 return rv
? rv
: len
;
426 static ssize_t
first_done_show(struct gfs2_sbd
*sdp
, char *buf
)
428 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
429 return sprintf(buf
, "%d\n", !!test_bit(DFL_FIRST_MOUNT_DONE
, &ls
->ls_recover_flags
));
432 int gfs2_recover_set(struct gfs2_sbd
*sdp
, unsigned jid
)
434 struct gfs2_jdesc
*jd
;
437 /* Wait for our primary journal to be initialized */
438 wait_for_completion(&sdp
->sd_journal_ready
);
440 spin_lock(&sdp
->sd_jindex_spin
);
442 if (sdp
->sd_jdesc
->jd_jid
== jid
)
445 list_for_each_entry(jd
, &sdp
->sd_jindex_list
, jd_list
) {
446 if (jd
->jd_jid
!= jid
)
448 rv
= gfs2_recover_journal(jd
, false);
452 spin_unlock(&sdp
->sd_jindex_spin
);
456 static ssize_t
recover_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
461 rv
= sscanf(buf
, "%u", &jid
);
465 if (test_bit(SDF_NORECOVERY
, &sdp
->sd_flags
)) {
470 rv
= gfs2_recover_set(sdp
, jid
);
472 return rv
? rv
: len
;
475 static ssize_t
recover_done_show(struct gfs2_sbd
*sdp
, char *buf
)
477 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
478 return sprintf(buf
, "%d\n", ls
->ls_recover_jid_done
);
481 static ssize_t
recover_status_show(struct gfs2_sbd
*sdp
, char *buf
)
483 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
484 return sprintf(buf
, "%d\n", ls
->ls_recover_jid_status
);
487 static ssize_t
jid_show(struct gfs2_sbd
*sdp
, char *buf
)
489 return sprintf(buf
, "%d\n", sdp
->sd_lockstruct
.ls_jid
);
492 static ssize_t
jid_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
497 rv
= sscanf(buf
, "%d", &jid
);
500 rv
= wait_for_completion_killable(&sdp
->sd_locking_init
);
503 spin_lock(&sdp
->sd_jindex_spin
);
505 if (sdp
->sd_lockstruct
.ls_ops
->lm_mount
== NULL
)
508 if (test_bit(SDF_NOJOURNALID
, &sdp
->sd_flags
) == 0)
511 if (sdp
->sd_args
.ar_spectator
&& jid
> 0)
513 sdp
->sd_lockstruct
.ls_jid
= jid
;
514 clear_bit(SDF_NOJOURNALID
, &sdp
->sd_flags
);
515 smp_mb__after_atomic();
516 wake_up_bit(&sdp
->sd_flags
, SDF_NOJOURNALID
);
518 spin_unlock(&sdp
->sd_jindex_spin
);
519 return rv
? rv
: len
;
522 #define GDLM_ATTR(_name,_mode,_show,_store) \
523 static struct gfs2_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
525 GDLM_ATTR(proto_name
, 0444, proto_name_show
, NULL
);
526 GDLM_ATTR(block
, 0644, block_show
, block_store
);
527 GDLM_ATTR(withdraw
, 0644, wdack_show
, wdack_store
);
528 GDLM_ATTR(jid
, 0644, jid_show
, jid_store
);
529 GDLM_ATTR(first
, 0644, lkfirst_show
, lkfirst_store
);
530 GDLM_ATTR(first_done
, 0444, first_done_show
, NULL
);
531 GDLM_ATTR(recover
, 0600, NULL
, recover_store
);
532 GDLM_ATTR(recover_done
, 0444, recover_done_show
, NULL
);
533 GDLM_ATTR(recover_status
, 0444, recover_status_show
, NULL
);
535 static struct attribute
*lock_module_attrs
[] = {
536 &gdlm_attr_proto_name
.attr
,
537 &gdlm_attr_block
.attr
,
538 &gdlm_attr_withdraw
.attr
,
540 &gdlm_attr_first
.attr
,
541 &gdlm_attr_first_done
.attr
,
542 &gdlm_attr_recover
.attr
,
543 &gdlm_attr_recover_done
.attr
,
544 &gdlm_attr_recover_status
.attr
,
549 * get and set struct gfs2_tune fields
552 static ssize_t
quota_scale_show(struct gfs2_sbd
*sdp
, char *buf
)
554 return snprintf(buf
, PAGE_SIZE
, "%u %u\n",
555 sdp
->sd_tune
.gt_quota_scale_num
,
556 sdp
->sd_tune
.gt_quota_scale_den
);
559 static ssize_t
quota_scale_store(struct gfs2_sbd
*sdp
, const char *buf
,
562 struct gfs2_tune
*gt
= &sdp
->sd_tune
;
565 if (!capable(CAP_SYS_ADMIN
))
568 if (sscanf(buf
, "%u %u", &x
, &y
) != 2 || !y
)
571 spin_lock(>
->gt_spin
);
572 gt
->gt_quota_scale_num
= x
;
573 gt
->gt_quota_scale_den
= y
;
574 spin_unlock(>
->gt_spin
);
578 static ssize_t
tune_set(struct gfs2_sbd
*sdp
, unsigned int *field
,
579 int check_zero
, const char *buf
, size_t len
)
581 struct gfs2_tune
*gt
= &sdp
->sd_tune
;
585 if (!capable(CAP_SYS_ADMIN
))
588 error
= kstrtouint(buf
, 0, &x
);
592 if (check_zero
&& !x
)
595 spin_lock(>
->gt_spin
);
597 spin_unlock(>
->gt_spin
);
601 #define TUNE_ATTR_3(name, show, store) \
602 static struct gfs2_attr tune_attr_##name = __ATTR(name, 0644, show, store)
604 #define TUNE_ATTR_2(name, store) \
605 static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf) \
607 return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name); \
609 TUNE_ATTR_3(name, name##_show, store)
611 #define TUNE_ATTR(name, check_zero) \
612 static ssize_t name##_store(struct gfs2_sbd *sdp, const char *buf, size_t len)\
614 return tune_set(sdp, &sdp->sd_tune.gt_##name, check_zero, buf, len); \
616 TUNE_ATTR_2(name, name##_store)
618 TUNE_ATTR(quota_warn_period
, 0);
619 TUNE_ATTR(quota_quantum
, 0);
620 TUNE_ATTR(max_readahead
, 0);
621 TUNE_ATTR(complain_secs
, 0);
622 TUNE_ATTR(statfs_slow
, 0);
623 TUNE_ATTR(new_files_jdata
, 0);
624 TUNE_ATTR(statfs_quantum
, 1);
625 TUNE_ATTR_3(quota_scale
, quota_scale_show
, quota_scale_store
);
627 static struct attribute
*tune_attrs
[] = {
628 &tune_attr_quota_warn_period
.attr
,
629 &tune_attr_quota_quantum
.attr
,
630 &tune_attr_max_readahead
.attr
,
631 &tune_attr_complain_secs
.attr
,
632 &tune_attr_statfs_slow
.attr
,
633 &tune_attr_statfs_quantum
.attr
,
634 &tune_attr_quota_scale
.attr
,
635 &tune_attr_new_files_jdata
.attr
,
639 static struct attribute_group tune_group
= {
644 static struct attribute_group lock_module_group
= {
645 .name
= "lock_module",
646 .attrs
= lock_module_attrs
,
649 int gfs2_sys_fs_add(struct gfs2_sbd
*sdp
)
651 struct super_block
*sb
= sdp
->sd_vfs
;
655 char *envp
[] = { ro
, spectator
, NULL
};
656 int sysfs_frees_sdp
= 0;
658 sprintf(ro
, "RDONLY=%d", (sb
->s_flags
& MS_RDONLY
) ? 1 : 0);
659 sprintf(spectator
, "SPECTATOR=%d", sdp
->sd_args
.ar_spectator
? 1 : 0);
661 sdp
->sd_kobj
.kset
= gfs2_kset
;
662 error
= kobject_init_and_add(&sdp
->sd_kobj
, &gfs2_ktype
, NULL
,
663 "%s", sdp
->sd_table_name
);
667 sysfs_frees_sdp
= 1; /* Freeing sdp is now done by sysfs calling
668 function gfs2_sbd_release. */
669 error
= sysfs_create_group(&sdp
->sd_kobj
, &tune_group
);
673 error
= sysfs_create_group(&sdp
->sd_kobj
, &lock_module_group
);
677 error
= sysfs_create_link(&sdp
->sd_kobj
,
678 &disk_to_dev(sb
->s_bdev
->bd_disk
)->kobj
,
681 goto fail_lock_module
;
683 kobject_uevent_env(&sdp
->sd_kobj
, KOBJ_ADD
, envp
);
687 sysfs_remove_group(&sdp
->sd_kobj
, &lock_module_group
);
689 sysfs_remove_group(&sdp
->sd_kobj
, &tune_group
);
691 free_percpu(sdp
->sd_lkstats
);
692 fs_err(sdp
, "error %d adding sysfs files", error
);
694 kobject_put(&sdp
->sd_kobj
);
697 sb
->s_fs_info
= NULL
;
701 void gfs2_sys_fs_del(struct gfs2_sbd
*sdp
)
703 sysfs_remove_link(&sdp
->sd_kobj
, "device");
704 sysfs_remove_group(&sdp
->sd_kobj
, &tune_group
);
705 sysfs_remove_group(&sdp
->sd_kobj
, &lock_module_group
);
706 kobject_put(&sdp
->sd_kobj
);
709 static int gfs2_uevent(struct kset
*kset
, struct kobject
*kobj
,
710 struct kobj_uevent_env
*env
)
712 struct gfs2_sbd
*sdp
= container_of(kobj
, struct gfs2_sbd
, sd_kobj
);
713 struct super_block
*s
= sdp
->sd_vfs
;
714 const u8
*uuid
= s
->s_uuid
;
716 add_uevent_var(env
, "LOCKTABLE=%s", sdp
->sd_table_name
);
717 add_uevent_var(env
, "LOCKPROTO=%s", sdp
->sd_proto_name
);
718 if (!test_bit(SDF_NOJOURNALID
, &sdp
->sd_flags
))
719 add_uevent_var(env
, "JOURNALID=%d", sdp
->sd_lockstruct
.ls_jid
);
720 if (gfs2_uuid_valid(uuid
))
721 add_uevent_var(env
, "UUID=%pUB", uuid
);
725 static const struct kset_uevent_ops gfs2_uevent_ops
= {
726 .uevent
= gfs2_uevent
,
729 int gfs2_sys_init(void)
731 gfs2_kset
= kset_create_and_add("gfs2", &gfs2_uevent_ops
, fs_kobj
);
737 void gfs2_sys_uninit(void)
739 kset_unregister(gfs2_kset
);