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>
29 static ssize_t id_show(struct gfs2_sbd *sdp, char *buf)
31 return snprintf(buf, PAGE_SIZE, "%u:%u\n",
32 MAJOR(sdp->sd_vfs->s_dev), MINOR(sdp->sd_vfs->s_dev));
35 static ssize_t fsname_show(struct gfs2_sbd *sdp, char *buf)
37 return snprintf(buf, PAGE_SIZE, "%s\n", sdp->sd_fsname);
40 static int gfs2_uuid_valid(const u8 *uuid)
44 for (i = 0; i < 16; i++) {
51 static ssize_t uuid_show(struct gfs2_sbd *sdp, char *buf)
53 const u8 *uuid = sdp->sd_sb.sb_uuid;
55 if (!gfs2_uuid_valid(uuid))
57 return snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X-%02X%02X-"
58 "%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
59 uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5],
60 uuid[6], uuid[7], uuid[8], uuid[9], uuid[10], uuid[11],
61 uuid[12], uuid[13], uuid[14], uuid[15]);
64 static ssize_t freeze_show(struct gfs2_sbd *sdp, char *buf)
68 mutex_lock(&sdp->sd_freeze_lock);
69 count = sdp->sd_freeze_count;
70 mutex_unlock(&sdp->sd_freeze_lock);
72 return snprintf(buf, PAGE_SIZE, "%u\n", count);
75 static ssize_t freeze_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
79 int n = simple_strtol(buf, NULL, 0);
81 if (!capable(CAP_SYS_ADMIN))
86 gfs2_unfreeze_fs(sdp);
89 error = gfs2_freeze_fs(sdp);
96 fs_warn(sdp, "freeze %d error %d", n, error);
101 static ssize_t withdraw_show(struct gfs2_sbd *sdp, char *buf)
103 unsigned int b = test_bit(SDF_SHUTDOWN, &sdp->sd_flags);
104 return snprintf(buf, PAGE_SIZE, "%u\n", b);
107 static ssize_t withdraw_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
109 if (!capable(CAP_SYS_ADMIN))
112 if (simple_strtol(buf, NULL, 0) != 1)
115 gfs2_lm_withdraw(sdp,
116 "GFS2: fsid=%s: withdrawing from cluster at user's request\n",
121 static ssize_t statfs_sync_store(struct gfs2_sbd *sdp, const char *buf,
124 if (!capable(CAP_SYS_ADMIN))
127 if (simple_strtol(buf, NULL, 0) != 1)
130 gfs2_statfs_sync(sdp);
134 static ssize_t quota_sync_store(struct gfs2_sbd *sdp, const char *buf,
137 if (!capable(CAP_SYS_ADMIN))
140 if (simple_strtol(buf, NULL, 0) != 1)
143 gfs2_quota_sync(sdp);
147 static ssize_t quota_refresh_user_store(struct gfs2_sbd *sdp, const char *buf,
152 if (!capable(CAP_SYS_ADMIN))
155 id = simple_strtoul(buf, NULL, 0);
157 gfs2_quota_refresh(sdp, 1, id);
161 static ssize_t quota_refresh_group_store(struct gfs2_sbd *sdp, const char *buf,
166 if (!capable(CAP_SYS_ADMIN))
169 id = simple_strtoul(buf, NULL, 0);
171 gfs2_quota_refresh(sdp, 0, id);
175 static ssize_t demote_rq_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
177 struct gfs2_glock *gl;
178 const struct gfs2_glock_operations *glops;
181 unsigned long long glnum;
185 if (!capable(CAP_SYS_ADMIN))
188 rv = sscanf(buf, "%u:%llu %15s", &gltype, &glnum,
193 if (strcmp(mode, "EX") == 0)
194 glmode = LM_ST_UNLOCKED;
195 else if ((strcmp(mode, "CW") == 0) || (strcmp(mode, "DF") == 0))
196 glmode = LM_ST_DEFERRED;
197 else if ((strcmp(mode, "PR") == 0) || (strcmp(mode, "SH") == 0))
198 glmode = LM_ST_SHARED;
202 if (gltype > LM_TYPE_JOURNAL)
204 glops = gfs2_glops_list[gltype];
207 rv = gfs2_glock_get(sdp, glnum, glops, 0, &gl);
210 gfs2_glock_cb(gl, glmode);
216 struct attribute attr;
217 ssize_t (*show)(struct gfs2_sbd *, char *);
218 ssize_t (*store)(struct gfs2_sbd *, const char *, size_t);
221 #define GFS2_ATTR(name, mode, show, store) \
222 static struct gfs2_attr gfs2_attr_##name = __ATTR(name, mode, show, store)
224 GFS2_ATTR(id, 0444, id_show, NULL);
225 GFS2_ATTR(fsname, 0444, fsname_show, NULL);
226 GFS2_ATTR(uuid, 0444, uuid_show, NULL);
227 GFS2_ATTR(freeze, 0644, freeze_show, freeze_store);
228 GFS2_ATTR(withdraw, 0644, withdraw_show, withdraw_store);
229 GFS2_ATTR(statfs_sync, 0200, NULL, statfs_sync_store);
230 GFS2_ATTR(quota_sync, 0200, NULL, quota_sync_store);
231 GFS2_ATTR(quota_refresh_user, 0200, NULL, quota_refresh_user_store);
232 GFS2_ATTR(quota_refresh_group, 0200, NULL, quota_refresh_group_store);
233 GFS2_ATTR(demote_rq, 0200, NULL, demote_rq_store);
235 static struct attribute *gfs2_attrs[] = {
237 &gfs2_attr_fsname.attr,
238 &gfs2_attr_uuid.attr,
239 &gfs2_attr_freeze.attr,
240 &gfs2_attr_withdraw.attr,
241 &gfs2_attr_statfs_sync.attr,
242 &gfs2_attr_quota_sync.attr,
243 &gfs2_attr_quota_refresh_user.attr,
244 &gfs2_attr_quota_refresh_group.attr,
245 &gfs2_attr_demote_rq.attr,
249 static ssize_t gfs2_attr_show(struct kobject *kobj, struct attribute *attr,
252 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
253 struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
254 return a->show ? a->show(sdp, buf) : 0;
257 static ssize_t gfs2_attr_store(struct kobject *kobj, struct attribute *attr,
258 const char *buf, size_t len)
260 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
261 struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
262 return a->store ? a->store(sdp, buf, len) : len;
265 static struct sysfs_ops gfs2_attr_ops = {
266 .show = gfs2_attr_show,
267 .store = gfs2_attr_store,
270 static struct kobj_type gfs2_ktype = {
271 .default_attrs = gfs2_attrs,
272 .sysfs_ops = &gfs2_attr_ops,
275 static struct kset *gfs2_kset;
278 * display struct lm_lockstruct fields
281 struct lockstruct_attr {
282 struct attribute attr;
283 ssize_t (*show)(struct gfs2_sbd *, char *);
286 #define LOCKSTRUCT_ATTR(name, fmt) \
287 static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf) \
289 return snprintf(buf, PAGE_SIZE, fmt, sdp->sd_lockstruct.ls_##name); \
291 static struct lockstruct_attr lockstruct_attr_##name = __ATTR_RO(name)
293 LOCKSTRUCT_ATTR(jid, "%u\n");
294 LOCKSTRUCT_ATTR(first, "%u\n");
296 static struct attribute *lockstruct_attrs[] = {
297 &lockstruct_attr_jid.attr,
298 &lockstruct_attr_first.attr,
303 * lock_module. Originally from lock_dlm
306 static ssize_t proto_name_show(struct gfs2_sbd *sdp, char *buf)
308 const struct lm_lockops *ops = sdp->sd_lockstruct.ls_ops;
309 return sprintf(buf, "%s\n", ops->lm_proto_name);
312 static ssize_t block_show(struct gfs2_sbd *sdp, char *buf)
314 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
318 if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_flags))
320 ret = sprintf(buf, "%d\n", val);
324 static ssize_t block_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
326 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
330 val = simple_strtol(buf, NULL, 0);
333 set_bit(DFL_BLOCK_LOCKS, &ls->ls_flags);
335 clear_bit(DFL_BLOCK_LOCKS, &ls->ls_flags);
336 smp_mb__after_clear_bit();
337 gfs2_glock_thaw(sdp);
344 static ssize_t lkid_show(struct gfs2_sbd *sdp, char *buf)
346 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
347 return sprintf(buf, "%u\n", ls->ls_id);
350 static ssize_t lkfirst_show(struct gfs2_sbd *sdp, char *buf)
352 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
353 return sprintf(buf, "%d\n", ls->ls_first);
356 static ssize_t first_done_show(struct gfs2_sbd *sdp, char *buf)
358 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
359 return sprintf(buf, "%d\n", ls->ls_first_done);
362 static ssize_t recover_show(struct gfs2_sbd *sdp, char *buf)
364 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
365 return sprintf(buf, "%d\n", ls->ls_recover_jid);
368 static void gfs2_jdesc_make_dirty(struct gfs2_sbd *sdp, unsigned int jid)
370 struct gfs2_jdesc *jd;
372 spin_lock(&sdp->sd_jindex_spin);
373 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
374 if (jd->jd_jid != jid)
379 spin_unlock(&sdp->sd_jindex_spin);
382 static ssize_t recover_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
384 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
385 ls->ls_recover_jid = simple_strtol(buf, NULL, 0);
386 gfs2_jdesc_make_dirty(sdp, ls->ls_recover_jid);
387 if (sdp->sd_recoverd_process)
388 wake_up_process(sdp->sd_recoverd_process);
392 static ssize_t recover_done_show(struct gfs2_sbd *sdp, char *buf)
394 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
395 return sprintf(buf, "%d\n", ls->ls_recover_jid_done);
398 static ssize_t recover_status_show(struct gfs2_sbd *sdp, char *buf)
400 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
401 return sprintf(buf, "%d\n", ls->ls_recover_jid_status);
405 struct attribute attr;
406 ssize_t (*show)(struct gfs2_sbd *sdp, char *);
407 ssize_t (*store)(struct gfs2_sbd *sdp, const char *, size_t);
410 #define GDLM_ATTR(_name,_mode,_show,_store) \
411 static struct gdlm_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
413 GDLM_ATTR(proto_name, 0444, proto_name_show, NULL);
414 GDLM_ATTR(block, 0644, block_show, block_store);
415 GDLM_ATTR(withdraw, 0644, withdraw_show, withdraw_store);
416 GDLM_ATTR(id, 0444, lkid_show, NULL);
417 GDLM_ATTR(first, 0444, lkfirst_show, NULL);
418 GDLM_ATTR(first_done, 0444, first_done_show, NULL);
419 GDLM_ATTR(recover, 0644, recover_show, recover_store);
420 GDLM_ATTR(recover_done, 0444, recover_done_show, NULL);
421 GDLM_ATTR(recover_status, 0444, recover_status_show, NULL);
423 static struct attribute *lock_module_attrs[] = {
424 &gdlm_attr_proto_name.attr,
425 &gdlm_attr_block.attr,
426 &gdlm_attr_withdraw.attr,
428 &lockstruct_attr_jid.attr,
429 &gdlm_attr_first.attr,
430 &gdlm_attr_first_done.attr,
431 &gdlm_attr_recover.attr,
432 &gdlm_attr_recover_done.attr,
433 &gdlm_attr_recover_status.attr,
438 * display struct gfs2_args fields
442 struct attribute attr;
443 ssize_t (*show)(struct gfs2_sbd *, char *);
446 #define ARGS_ATTR(name, fmt) \
447 static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf) \
449 return snprintf(buf, PAGE_SIZE, fmt, sdp->sd_args.ar_##name); \
451 static struct args_attr args_attr_##name = __ATTR_RO(name)
453 ARGS_ATTR(lockproto, "%s\n");
454 ARGS_ATTR(locktable, "%s\n");
455 ARGS_ATTR(hostdata, "%s\n");
456 ARGS_ATTR(spectator, "%d\n");
457 ARGS_ATTR(ignore_local_fs, "%d\n");
458 ARGS_ATTR(localcaching, "%d\n");
459 ARGS_ATTR(localflocks, "%d\n");
460 ARGS_ATTR(debug, "%d\n");
461 ARGS_ATTR(upgrade, "%d\n");
462 ARGS_ATTR(posix_acl, "%d\n");
463 ARGS_ATTR(quota, "%u\n");
464 ARGS_ATTR(suiddir, "%d\n");
465 ARGS_ATTR(data, "%d\n");
467 static struct attribute *args_attrs[] = {
468 &args_attr_lockproto.attr,
469 &args_attr_locktable.attr,
470 &args_attr_hostdata.attr,
471 &args_attr_spectator.attr,
472 &args_attr_ignore_local_fs.attr,
473 &args_attr_localcaching.attr,
474 &args_attr_localflocks.attr,
475 &args_attr_debug.attr,
476 &args_attr_upgrade.attr,
477 &args_attr_posix_acl.attr,
478 &args_attr_quota.attr,
479 &args_attr_suiddir.attr,
480 &args_attr_data.attr,
485 * get and set struct gfs2_tune fields
488 static ssize_t quota_scale_show(struct gfs2_sbd *sdp, char *buf)
490 return snprintf(buf, PAGE_SIZE, "%u %u\n",
491 sdp->sd_tune.gt_quota_scale_num,
492 sdp->sd_tune.gt_quota_scale_den);
495 static ssize_t quota_scale_store(struct gfs2_sbd *sdp, const char *buf,
498 struct gfs2_tune *gt = &sdp->sd_tune;
501 if (!capable(CAP_SYS_ADMIN))
504 if (sscanf(buf, "%u %u", &x, &y) != 2 || !y)
507 spin_lock(>->gt_spin);
508 gt->gt_quota_scale_num = x;
509 gt->gt_quota_scale_den = y;
510 spin_unlock(>->gt_spin);
514 static ssize_t tune_set(struct gfs2_sbd *sdp, unsigned int *field,
515 int check_zero, const char *buf, size_t len)
517 struct gfs2_tune *gt = &sdp->sd_tune;
520 if (!capable(CAP_SYS_ADMIN))
523 x = simple_strtoul(buf, NULL, 0);
525 if (check_zero && !x)
528 spin_lock(>->gt_spin);
530 spin_unlock(>->gt_spin);
535 struct attribute attr;
536 ssize_t (*show)(struct gfs2_sbd *, char *);
537 ssize_t (*store)(struct gfs2_sbd *, const char *, size_t);
540 #define TUNE_ATTR_3(name, show, store) \
541 static struct tune_attr tune_attr_##name = __ATTR(name, 0644, show, store)
543 #define TUNE_ATTR_2(name, store) \
544 static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf) \
546 return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name); \
548 TUNE_ATTR_3(name, name##_show, store)
550 #define TUNE_ATTR(name, check_zero) \
551 static ssize_t name##_store(struct gfs2_sbd *sdp, const char *buf, size_t len)\
553 return tune_set(sdp, &sdp->sd_tune.gt_##name, check_zero, buf, len); \
555 TUNE_ATTR_2(name, name##_store)
557 #define TUNE_ATTR_DAEMON(name, process) \
558 static ssize_t name##_store(struct gfs2_sbd *sdp, const char *buf, size_t len)\
560 ssize_t r = tune_set(sdp, &sdp->sd_tune.gt_##name, 1, buf, len); \
561 wake_up_process(sdp->sd_##process); \
564 TUNE_ATTR_2(name, name##_store)
566 TUNE_ATTR(incore_log_blocks, 0);
567 TUNE_ATTR(log_flush_secs, 0);
568 TUNE_ATTR(quota_warn_period, 0);
569 TUNE_ATTR(quota_quantum, 0);
570 TUNE_ATTR(max_readahead, 0);
571 TUNE_ATTR(complain_secs, 0);
572 TUNE_ATTR(statfs_slow, 0);
573 TUNE_ATTR(new_files_jdata, 0);
574 TUNE_ATTR(quota_simul_sync, 1);
575 TUNE_ATTR(stall_secs, 1);
576 TUNE_ATTR(statfs_quantum, 1);
577 TUNE_ATTR_DAEMON(recoverd_secs, recoverd_process);
578 TUNE_ATTR_DAEMON(logd_secs, logd_process);
579 TUNE_ATTR_3(quota_scale, quota_scale_show, quota_scale_store);
581 static struct attribute *tune_attrs[] = {
582 &tune_attr_incore_log_blocks.attr,
583 &tune_attr_log_flush_secs.attr,
584 &tune_attr_quota_warn_period.attr,
585 &tune_attr_quota_quantum.attr,
586 &tune_attr_max_readahead.attr,
587 &tune_attr_complain_secs.attr,
588 &tune_attr_statfs_slow.attr,
589 &tune_attr_quota_simul_sync.attr,
590 &tune_attr_stall_secs.attr,
591 &tune_attr_statfs_quantum.attr,
592 &tune_attr_recoverd_secs.attr,
593 &tune_attr_logd_secs.attr,
594 &tune_attr_quota_scale.attr,
595 &tune_attr_new_files_jdata.attr,
599 static struct attribute_group lockstruct_group = {
600 .name = "lockstruct",
601 .attrs = lockstruct_attrs,
604 static struct attribute_group args_group = {
609 static struct attribute_group tune_group = {
614 static struct attribute_group lock_module_group = {
615 .name = "lock_module",
616 .attrs = lock_module_attrs,
619 int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
623 sdp->sd_kobj.kset = gfs2_kset;
624 error = kobject_init_and_add(&sdp->sd_kobj, &gfs2_ktype, NULL,
625 "%s", sdp->sd_table_name);
629 error = sysfs_create_group(&sdp->sd_kobj, &lockstruct_group);
633 error = sysfs_create_group(&sdp->sd_kobj, &args_group);
635 goto fail_lockstruct;
637 error = sysfs_create_group(&sdp->sd_kobj, &tune_group);
641 error = sysfs_create_group(&sdp->sd_kobj, &lock_module_group);
645 kobject_uevent(&sdp->sd_kobj, KOBJ_ADD);
649 sysfs_remove_group(&sdp->sd_kobj, &tune_group);
651 sysfs_remove_group(&sdp->sd_kobj, &args_group);
653 sysfs_remove_group(&sdp->sd_kobj, &lockstruct_group);
655 kobject_put(&sdp->sd_kobj);
657 fs_err(sdp, "error %d adding sysfs files", error);
661 void gfs2_sys_fs_del(struct gfs2_sbd *sdp)
663 sysfs_remove_group(&sdp->sd_kobj, &tune_group);
664 sysfs_remove_group(&sdp->sd_kobj, &args_group);
665 sysfs_remove_group(&sdp->sd_kobj, &lockstruct_group);
666 sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
667 kobject_put(&sdp->sd_kobj);
671 static int gfs2_uevent(struct kset *kset, struct kobject *kobj,
672 struct kobj_uevent_env *env)
674 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
675 const u8 *uuid = sdp->sd_sb.sb_uuid;
677 add_uevent_var(env, "LOCKTABLE=%s", sdp->sd_table_name);
678 add_uevent_var(env, "LOCKPROTO=%s", sdp->sd_proto_name);
679 if (gfs2_uuid_valid(uuid)) {
680 add_uevent_var(env, "UUID=%02X%02X%02X%02X-%02X%02X-%02X%02X-"
681 "%02X%02X-%02X%02X%02X%02X%02X%02X",
682 uuid[0], uuid[1], uuid[2], uuid[3], uuid[4],
683 uuid[5], uuid[6], uuid[7], uuid[8], uuid[9],
684 uuid[10], uuid[11], uuid[12], uuid[13],
690 static struct kset_uevent_ops gfs2_uevent_ops = {
691 .uevent = gfs2_uevent,
695 int gfs2_sys_init(void)
697 gfs2_kset = kset_create_and_add("gfs2", &gfs2_uevent_ops, fs_kobj);
703 void gfs2_sys_uninit(void)
705 kset_unregister(gfs2_kset);