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 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/module.h>
16 #include <linux/kobject.h>
17 #include <asm/uaccess.h>
18 #include <linux/gfs2_ondisk.h>
30 struct attribute attr
;
31 ssize_t (*show
)(struct gfs2_sbd
*, char *);
32 ssize_t (*store
)(struct gfs2_sbd
*, const char *, size_t);
35 static ssize_t
gfs2_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
38 struct gfs2_sbd
*sdp
= container_of(kobj
, struct gfs2_sbd
, sd_kobj
);
39 struct gfs2_attr
*a
= container_of(attr
, struct gfs2_attr
, attr
);
40 return a
->show
? a
->show(sdp
, buf
) : 0;
43 static ssize_t
gfs2_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
44 const char *buf
, size_t len
)
46 struct gfs2_sbd
*sdp
= container_of(kobj
, struct gfs2_sbd
, sd_kobj
);
47 struct gfs2_attr
*a
= container_of(attr
, struct gfs2_attr
, attr
);
48 return a
->store
? a
->store(sdp
, buf
, len
) : len
;
51 static struct sysfs_ops gfs2_attr_ops
= {
52 .show
= gfs2_attr_show
,
53 .store
= gfs2_attr_store
,
57 static struct kset
*gfs2_kset
;
59 static ssize_t
id_show(struct gfs2_sbd
*sdp
, char *buf
)
61 return snprintf(buf
, PAGE_SIZE
, "%u:%u\n",
62 MAJOR(sdp
->sd_vfs
->s_dev
), MINOR(sdp
->sd_vfs
->s_dev
));
65 static ssize_t
fsname_show(struct gfs2_sbd
*sdp
, char *buf
)
67 return snprintf(buf
, PAGE_SIZE
, "%s\n", sdp
->sd_fsname
);
70 static int gfs2_uuid_valid(const u8
*uuid
)
74 for (i
= 0; i
< 16; i
++) {
81 static ssize_t
uuid_show(struct gfs2_sbd
*sdp
, char *buf
)
83 const u8
*uuid
= sdp
->sd_sb
.sb_uuid
;
85 if (!gfs2_uuid_valid(uuid
))
87 return snprintf(buf
, PAGE_SIZE
, "%02X%02X%02X%02X-%02X%02X-"
88 "%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
89 uuid
[0], uuid
[1], uuid
[2], uuid
[3], uuid
[4], uuid
[5],
90 uuid
[6], uuid
[7], uuid
[8], uuid
[9], uuid
[10], uuid
[11],
91 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
94 static ssize_t
freeze_show(struct gfs2_sbd
*sdp
, char *buf
)
98 mutex_lock(&sdp
->sd_freeze_lock
);
99 count
= sdp
->sd_freeze_count
;
100 mutex_unlock(&sdp
->sd_freeze_lock
);
102 return snprintf(buf
, PAGE_SIZE
, "%u\n", count
);
105 static ssize_t
freeze_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
109 int n
= simple_strtol(buf
, NULL
, 0);
111 if (!capable(CAP_SYS_ADMIN
))
116 gfs2_unfreeze_fs(sdp
);
119 error
= gfs2_freeze_fs(sdp
);
126 fs_warn(sdp
, "freeze %d error %d", n
, error
);
131 static ssize_t
withdraw_show(struct gfs2_sbd
*sdp
, char *buf
)
133 unsigned int b
= test_bit(SDF_SHUTDOWN
, &sdp
->sd_flags
);
134 return snprintf(buf
, PAGE_SIZE
, "%u\n", b
);
137 static ssize_t
withdraw_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
139 if (!capable(CAP_SYS_ADMIN
))
142 if (simple_strtol(buf
, NULL
, 0) != 1)
145 gfs2_lm_withdraw(sdp
,
146 "GFS2: fsid=%s: withdrawing from cluster at user's request\n",
151 static ssize_t
statfs_sync_store(struct gfs2_sbd
*sdp
, const char *buf
,
154 if (!capable(CAP_SYS_ADMIN
))
157 if (simple_strtol(buf
, NULL
, 0) != 1)
160 gfs2_statfs_sync(sdp
);
164 static ssize_t
quota_sync_store(struct gfs2_sbd
*sdp
, const char *buf
,
167 if (!capable(CAP_SYS_ADMIN
))
170 if (simple_strtol(buf
, NULL
, 0) != 1)
173 gfs2_quota_sync(sdp
);
177 static ssize_t
quota_refresh_user_store(struct gfs2_sbd
*sdp
, const char *buf
,
182 if (!capable(CAP_SYS_ADMIN
))
185 id
= simple_strtoul(buf
, NULL
, 0);
187 gfs2_quota_refresh(sdp
, 1, id
);
191 static ssize_t
quota_refresh_group_store(struct gfs2_sbd
*sdp
, const char *buf
,
196 if (!capable(CAP_SYS_ADMIN
))
199 id
= simple_strtoul(buf
, NULL
, 0);
201 gfs2_quota_refresh(sdp
, 0, id
);
205 static ssize_t
demote_rq_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
207 struct gfs2_glock
*gl
;
208 const struct gfs2_glock_operations
*glops
;
211 unsigned long long glnum
;
215 if (!capable(CAP_SYS_ADMIN
))
218 rv
= sscanf(buf
, "%u:%llu %15s", &gltype
, &glnum
,
223 if (strcmp(mode
, "EX") == 0)
224 glmode
= LM_ST_UNLOCKED
;
225 else if ((strcmp(mode
, "CW") == 0) || (strcmp(mode
, "DF") == 0))
226 glmode
= LM_ST_DEFERRED
;
227 else if ((strcmp(mode
, "PR") == 0) || (strcmp(mode
, "SH") == 0))
228 glmode
= LM_ST_SHARED
;
232 if (gltype
> LM_TYPE_JOURNAL
)
234 glops
= gfs2_glops_list
[gltype
];
237 rv
= gfs2_glock_get(sdp
, glnum
, glops
, 0, &gl
);
240 gfs2_glock_cb(gl
, glmode
);
246 #define GFS2_ATTR(name, mode, show, store) \
247 static struct gfs2_attr gfs2_attr_##name = __ATTR(name, mode, show, store)
249 GFS2_ATTR(id
, 0444, id_show
, NULL
);
250 GFS2_ATTR(fsname
, 0444, fsname_show
, NULL
);
251 GFS2_ATTR(uuid
, 0444, uuid_show
, NULL
);
252 GFS2_ATTR(freeze
, 0644, freeze_show
, freeze_store
);
253 GFS2_ATTR(withdraw
, 0644, withdraw_show
, withdraw_store
);
254 GFS2_ATTR(statfs_sync
, 0200, NULL
, statfs_sync_store
);
255 GFS2_ATTR(quota_sync
, 0200, NULL
, quota_sync_store
);
256 GFS2_ATTR(quota_refresh_user
, 0200, NULL
, quota_refresh_user_store
);
257 GFS2_ATTR(quota_refresh_group
, 0200, NULL
, quota_refresh_group_store
);
258 GFS2_ATTR(demote_rq
, 0200, NULL
, demote_rq_store
);
260 static struct attribute
*gfs2_attrs
[] = {
262 &gfs2_attr_fsname
.attr
,
263 &gfs2_attr_uuid
.attr
,
264 &gfs2_attr_freeze
.attr
,
265 &gfs2_attr_withdraw
.attr
,
266 &gfs2_attr_statfs_sync
.attr
,
267 &gfs2_attr_quota_sync
.attr
,
268 &gfs2_attr_quota_refresh_user
.attr
,
269 &gfs2_attr_quota_refresh_group
.attr
,
270 &gfs2_attr_demote_rq
.attr
,
274 static struct kobj_type gfs2_ktype
= {
275 .default_attrs
= gfs2_attrs
,
276 .sysfs_ops
= &gfs2_attr_ops
,
280 * display struct lm_lockstruct fields
283 #define LOCKSTRUCT_ATTR(name, fmt) \
284 static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf) \
286 return snprintf(buf, PAGE_SIZE, fmt, sdp->sd_lockstruct.ls_##name); \
288 static struct gfs2_attr lockstruct_attr_##name = __ATTR_RO(name)
290 LOCKSTRUCT_ATTR(jid
, "%u\n");
291 LOCKSTRUCT_ATTR(first
, "%u\n");
293 static struct attribute
*lockstruct_attrs
[] = {
294 &lockstruct_attr_jid
.attr
,
295 &lockstruct_attr_first
.attr
,
300 * lock_module. Originally from lock_dlm
303 static ssize_t
proto_name_show(struct gfs2_sbd
*sdp
, char *buf
)
305 const struct lm_lockops
*ops
= sdp
->sd_lockstruct
.ls_ops
;
306 return sprintf(buf
, "%s\n", ops
->lm_proto_name
);
309 static ssize_t
block_show(struct gfs2_sbd
*sdp
, char *buf
)
311 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
315 if (test_bit(DFL_BLOCK_LOCKS
, &ls
->ls_flags
))
317 ret
= sprintf(buf
, "%d\n", val
);
321 static ssize_t
block_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
323 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
327 val
= simple_strtol(buf
, NULL
, 0);
330 set_bit(DFL_BLOCK_LOCKS
, &ls
->ls_flags
);
332 clear_bit(DFL_BLOCK_LOCKS
, &ls
->ls_flags
);
333 smp_mb__after_clear_bit();
334 gfs2_glock_thaw(sdp
);
341 static ssize_t
lkid_show(struct gfs2_sbd
*sdp
, char *buf
)
343 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
344 return sprintf(buf
, "%u\n", ls
->ls_id
);
347 static ssize_t
lkfirst_show(struct gfs2_sbd
*sdp
, char *buf
)
349 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
350 return sprintf(buf
, "%d\n", ls
->ls_first
);
353 static ssize_t
first_done_show(struct gfs2_sbd
*sdp
, char *buf
)
355 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
356 return sprintf(buf
, "%d\n", ls
->ls_first_done
);
359 static ssize_t
recover_show(struct gfs2_sbd
*sdp
, char *buf
)
361 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
362 return sprintf(buf
, "%d\n", ls
->ls_recover_jid
);
365 static void gfs2_jdesc_make_dirty(struct gfs2_sbd
*sdp
, unsigned int jid
)
367 struct gfs2_jdesc
*jd
;
369 spin_lock(&sdp
->sd_jindex_spin
);
370 list_for_each_entry(jd
, &sdp
->sd_jindex_list
, jd_list
) {
371 if (jd
->jd_jid
!= jid
)
376 spin_unlock(&sdp
->sd_jindex_spin
);
379 static ssize_t
recover_store(struct gfs2_sbd
*sdp
, const char *buf
, size_t len
)
381 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
382 ls
->ls_recover_jid
= simple_strtol(buf
, NULL
, 0);
383 gfs2_jdesc_make_dirty(sdp
, ls
->ls_recover_jid
);
384 if (sdp
->sd_recoverd_process
)
385 wake_up_process(sdp
->sd_recoverd_process
);
389 static ssize_t
recover_done_show(struct gfs2_sbd
*sdp
, char *buf
)
391 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
392 return sprintf(buf
, "%d\n", ls
->ls_recover_jid_done
);
395 static ssize_t
recover_status_show(struct gfs2_sbd
*sdp
, char *buf
)
397 struct lm_lockstruct
*ls
= &sdp
->sd_lockstruct
;
398 return sprintf(buf
, "%d\n", ls
->ls_recover_jid_status
);
401 #define GDLM_ATTR(_name,_mode,_show,_store) \
402 static struct gfs2_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
404 GDLM_ATTR(proto_name
, 0444, proto_name_show
, NULL
);
405 GDLM_ATTR(block
, 0644, block_show
, block_store
);
406 GDLM_ATTR(withdraw
, 0644, withdraw_show
, withdraw_store
);
407 GDLM_ATTR(id
, 0444, lkid_show
, NULL
);
408 GDLM_ATTR(first
, 0444, lkfirst_show
, NULL
);
409 GDLM_ATTR(first_done
, 0444, first_done_show
, NULL
);
410 GDLM_ATTR(recover
, 0644, recover_show
, recover_store
);
411 GDLM_ATTR(recover_done
, 0444, recover_done_show
, NULL
);
412 GDLM_ATTR(recover_status
, 0444, recover_status_show
, NULL
);
414 static struct attribute
*lock_module_attrs
[] = {
415 &gdlm_attr_proto_name
.attr
,
416 &gdlm_attr_block
.attr
,
417 &gdlm_attr_withdraw
.attr
,
419 &lockstruct_attr_jid
.attr
,
420 &gdlm_attr_first
.attr
,
421 &gdlm_attr_first_done
.attr
,
422 &gdlm_attr_recover
.attr
,
423 &gdlm_attr_recover_done
.attr
,
424 &gdlm_attr_recover_status
.attr
,
428 #define ARGS_ATTR(name, fmt) \
429 static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf) \
431 return snprintf(buf, PAGE_SIZE, fmt, sdp->sd_args.ar_##name); \
433 static struct gfs2_attr args_attr_##name = __ATTR_RO(name)
435 ARGS_ATTR(lockproto
, "%s\n");
436 ARGS_ATTR(locktable
, "%s\n");
437 ARGS_ATTR(hostdata
, "%s\n");
438 ARGS_ATTR(spectator
, "%d\n");
439 ARGS_ATTR(ignore_local_fs
, "%d\n");
440 ARGS_ATTR(localcaching
, "%d\n");
441 ARGS_ATTR(localflocks
, "%d\n");
442 ARGS_ATTR(debug
, "%d\n");
443 ARGS_ATTR(upgrade
, "%d\n");
444 ARGS_ATTR(posix_acl
, "%d\n");
445 ARGS_ATTR(quota
, "%u\n");
446 ARGS_ATTR(suiddir
, "%d\n");
447 ARGS_ATTR(data
, "%d\n");
449 static struct attribute
*args_attrs
[] = {
450 &args_attr_lockproto
.attr
,
451 &args_attr_locktable
.attr
,
452 &args_attr_hostdata
.attr
,
453 &args_attr_spectator
.attr
,
454 &args_attr_ignore_local_fs
.attr
,
455 &args_attr_localcaching
.attr
,
456 &args_attr_localflocks
.attr
,
457 &args_attr_debug
.attr
,
458 &args_attr_upgrade
.attr
,
459 &args_attr_posix_acl
.attr
,
460 &args_attr_quota
.attr
,
461 &args_attr_suiddir
.attr
,
462 &args_attr_data
.attr
,
467 * get and set struct gfs2_tune fields
470 static ssize_t
quota_scale_show(struct gfs2_sbd
*sdp
, char *buf
)
472 return snprintf(buf
, PAGE_SIZE
, "%u %u\n",
473 sdp
->sd_tune
.gt_quota_scale_num
,
474 sdp
->sd_tune
.gt_quota_scale_den
);
477 static ssize_t
quota_scale_store(struct gfs2_sbd
*sdp
, const char *buf
,
480 struct gfs2_tune
*gt
= &sdp
->sd_tune
;
483 if (!capable(CAP_SYS_ADMIN
))
486 if (sscanf(buf
, "%u %u", &x
, &y
) != 2 || !y
)
489 spin_lock(>
->gt_spin
);
490 gt
->gt_quota_scale_num
= x
;
491 gt
->gt_quota_scale_den
= y
;
492 spin_unlock(>
->gt_spin
);
496 static ssize_t
tune_set(struct gfs2_sbd
*sdp
, unsigned int *field
,
497 int check_zero
, const char *buf
, size_t len
)
499 struct gfs2_tune
*gt
= &sdp
->sd_tune
;
502 if (!capable(CAP_SYS_ADMIN
))
505 x
= simple_strtoul(buf
, NULL
, 0);
507 if (check_zero
&& !x
)
510 spin_lock(>
->gt_spin
);
512 spin_unlock(>
->gt_spin
);
516 #define TUNE_ATTR_3(name, show, store) \
517 static struct gfs2_attr tune_attr_##name = __ATTR(name, 0644, show, store)
519 #define TUNE_ATTR_2(name, store) \
520 static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf) \
522 return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name); \
524 TUNE_ATTR_3(name, name##_show, store)
526 #define TUNE_ATTR(name, check_zero) \
527 static ssize_t name##_store(struct gfs2_sbd *sdp, const char *buf, size_t len)\
529 return tune_set(sdp, &sdp->sd_tune.gt_##name, check_zero, buf, len); \
531 TUNE_ATTR_2(name, name##_store)
533 TUNE_ATTR(incore_log_blocks
, 0);
534 TUNE_ATTR(log_flush_secs
, 0);
535 TUNE_ATTR(quota_warn_period
, 0);
536 TUNE_ATTR(quota_quantum
, 0);
537 TUNE_ATTR(max_readahead
, 0);
538 TUNE_ATTR(complain_secs
, 0);
539 TUNE_ATTR(statfs_slow
, 0);
540 TUNE_ATTR(new_files_jdata
, 0);
541 TUNE_ATTR(quota_simul_sync
, 1);
542 TUNE_ATTR(stall_secs
, 1);
543 TUNE_ATTR(statfs_quantum
, 1);
544 TUNE_ATTR_3(quota_scale
, quota_scale_show
, quota_scale_store
);
546 static struct attribute
*tune_attrs
[] = {
547 &tune_attr_incore_log_blocks
.attr
,
548 &tune_attr_log_flush_secs
.attr
,
549 &tune_attr_quota_warn_period
.attr
,
550 &tune_attr_quota_quantum
.attr
,
551 &tune_attr_max_readahead
.attr
,
552 &tune_attr_complain_secs
.attr
,
553 &tune_attr_statfs_slow
.attr
,
554 &tune_attr_quota_simul_sync
.attr
,
555 &tune_attr_stall_secs
.attr
,
556 &tune_attr_statfs_quantum
.attr
,
557 &tune_attr_quota_scale
.attr
,
558 &tune_attr_new_files_jdata
.attr
,
562 static struct attribute_group lockstruct_group
= {
563 .name
= "lockstruct",
564 .attrs
= lockstruct_attrs
,
567 static struct attribute_group args_group
= {
572 static struct attribute_group tune_group
= {
577 static struct attribute_group lock_module_group
= {
578 .name
= "lock_module",
579 .attrs
= lock_module_attrs
,
582 int gfs2_sys_fs_add(struct gfs2_sbd
*sdp
)
586 sdp
->sd_kobj
.kset
= gfs2_kset
;
587 error
= kobject_init_and_add(&sdp
->sd_kobj
, &gfs2_ktype
, NULL
,
588 "%s", sdp
->sd_table_name
);
592 error
= sysfs_create_group(&sdp
->sd_kobj
, &lockstruct_group
);
596 error
= sysfs_create_group(&sdp
->sd_kobj
, &args_group
);
598 goto fail_lockstruct
;
600 error
= sysfs_create_group(&sdp
->sd_kobj
, &tune_group
);
604 error
= sysfs_create_group(&sdp
->sd_kobj
, &lock_module_group
);
608 kobject_uevent(&sdp
->sd_kobj
, KOBJ_ADD
);
612 sysfs_remove_group(&sdp
->sd_kobj
, &tune_group
);
614 sysfs_remove_group(&sdp
->sd_kobj
, &args_group
);
616 sysfs_remove_group(&sdp
->sd_kobj
, &lockstruct_group
);
618 kobject_put(&sdp
->sd_kobj
);
620 fs_err(sdp
, "error %d adding sysfs files", error
);
624 void gfs2_sys_fs_del(struct gfs2_sbd
*sdp
)
626 sysfs_remove_group(&sdp
->sd_kobj
, &tune_group
);
627 sysfs_remove_group(&sdp
->sd_kobj
, &args_group
);
628 sysfs_remove_group(&sdp
->sd_kobj
, &lockstruct_group
);
629 sysfs_remove_group(&sdp
->sd_kobj
, &lock_module_group
);
630 kobject_put(&sdp
->sd_kobj
);
634 static int gfs2_uevent(struct kset
*kset
, struct kobject
*kobj
,
635 struct kobj_uevent_env
*env
)
637 struct gfs2_sbd
*sdp
= container_of(kobj
, struct gfs2_sbd
, sd_kobj
);
638 const u8
*uuid
= sdp
->sd_sb
.sb_uuid
;
640 add_uevent_var(env
, "LOCKTABLE=%s", sdp
->sd_table_name
);
641 add_uevent_var(env
, "LOCKPROTO=%s", sdp
->sd_proto_name
);
642 if (gfs2_uuid_valid(uuid
)) {
643 add_uevent_var(env
, "UUID=%02X%02X%02X%02X-%02X%02X-%02X%02X-"
644 "%02X%02X-%02X%02X%02X%02X%02X%02X",
645 uuid
[0], uuid
[1], uuid
[2], uuid
[3], uuid
[4],
646 uuid
[5], uuid
[6], uuid
[7], uuid
[8], uuid
[9],
647 uuid
[10], uuid
[11], uuid
[12], uuid
[13],
653 static struct kset_uevent_ops gfs2_uevent_ops
= {
654 .uevent
= gfs2_uevent
,
658 int gfs2_sys_init(void)
660 gfs2_kset
= kset_create_and_add("gfs2", &gfs2_uevent_ops
, fs_kobj
);
666 void gfs2_sys_uninit(void)
668 kset_unregister(gfs2_kset
);