linux/fs/gfs2/sys.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
   3 * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
   4 *
   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.
   8 */
   9
  10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11
  12#include <linux/sched.h>
  13#include <linux/cred.h>
  14#include <linux/spinlock.h>
  15#include <linux/completion.h>
  16#include <linux/buffer_head.h>
  17#include <linux/module.h>
  18#include <linux/kobject.h>
  19#include <linux/uaccess.h>
  20#include <linux/gfs2_ondisk.h>
  21#include <linux/genhd.h>
  22
  23#include "gfs2.h"
  24#include "incore.h"
  25#include "sys.h"
  26#include "super.h"
  27#include "glock.h"
  28#include "quota.h"
  29#include "util.h"
  30#include "glops.h"
  31#include "recovery.h"
  32
  33struct gfs2_attr {
  34        struct attribute attr;
  35        ssize_t (*show)(struct gfs2_sbd *, char *);
  36        ssize_t (*store)(struct gfs2_sbd *, const char *, size_t);
  37};
  38
  39static ssize_t gfs2_attr_show(struct kobject *kobj, struct attribute *attr,
  40                              char *buf)
  41{
  42        struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
  43        struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
  44        return a->show ? a->show(sdp, buf) : 0;
  45}
  46
  47static ssize_t gfs2_attr_store(struct kobject *kobj, struct attribute *attr,
  48                               const char *buf, size_t len)
  49{
  50        struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
  51        struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
  52        return a->store ? a->store(sdp, buf, len) : len;
  53}
  54
  55static const struct sysfs_ops gfs2_attr_ops = {
  56        .show  = gfs2_attr_show,
  57        .store = gfs2_attr_store,
  58};
  59
  60
  61static struct kset *gfs2_kset;
  62
  63static ssize_t id_show(struct gfs2_sbd *sdp, char *buf)
  64{
  65        return snprintf(buf, PAGE_SIZE, "%u:%u\n",
  66                        MAJOR(sdp->sd_vfs->s_dev), MINOR(sdp->sd_vfs->s_dev));
  67}
  68
  69static ssize_t status_show(struct gfs2_sbd *sdp, char *buf)
  70{
  71        unsigned long f = sdp->sd_flags;
  72        ssize_t s;
  73
  74        s = snprintf(buf, PAGE_SIZE,
  75                     "Journal Checked:          %d\n"
  76                     "Journal Live:             %d\n"
  77                     "Journal ID:               %d\n"
  78                     "Spectator:                %d\n"
  79                     "Withdrawn:                %d\n"
  80                     "No barriers:              %d\n"
  81                     "No recovery:              %d\n"
  82                     "Demote:                   %d\n"
  83                     "No Journal ID:            %d\n"
  84                     "Mounted RO:               %d\n"
  85                     "RO Recovery:              %d\n"
  86                     "Skip DLM Unlock:          %d\n"
  87                     "Force AIL Flush:          %d\n"
  88                     "FS Frozen:                %d\n"
  89                     "Withdrawing:              %d\n"
  90                     "Withdraw In Prog:         %d\n"
  91                     "Remote Withdraw:          %d\n"
  92                     "Withdraw Recovery:        %d\n"
  93                     "sd_log_error:             %d\n"
  94                     "sd_log_flush_lock:        %d\n"
  95                     "sd_log_num_revoke:        %u\n"
  96                     "sd_log_in_flight:         %d\n"
  97                     "sd_log_blks_needed:       %d\n"
  98                     "sd_log_blks_free:         %d\n"
  99                     "sd_log_flush_head:        %d\n"
 100                     "sd_log_flush_tail:        %d\n"
 101                     "sd_log_blks_reserved:     %d\n"
 102                     "sd_log_revokes_available: %d\n",
 103                     test_bit(SDF_JOURNAL_CHECKED, &f),
 104                     test_bit(SDF_JOURNAL_LIVE, &f),
 105                     (sdp->sd_jdesc ? sdp->sd_jdesc->jd_jid : 0),
 106                     (sdp->sd_args.ar_spectator ? 1 : 0),
 107                     test_bit(SDF_WITHDRAWN, &f),
 108                     test_bit(SDF_NOBARRIERS, &f),
 109                     test_bit(SDF_NORECOVERY, &f),
 110                     test_bit(SDF_DEMOTE, &f),
 111                     test_bit(SDF_NOJOURNALID, &f),
 112                     (sb_rdonly(sdp->sd_vfs) ? 1 : 0),
 113                     test_bit(SDF_RORECOVERY, &f),
 114                     test_bit(SDF_SKIP_DLM_UNLOCK, &f),
 115                     test_bit(SDF_FORCE_AIL_FLUSH, &f),
 116                     test_bit(SDF_FS_FROZEN, &f),
 117                     test_bit(SDF_WITHDRAWING, &f),
 118                     test_bit(SDF_WITHDRAW_IN_PROG, &f),
 119                     test_bit(SDF_REMOTE_WITHDRAW, &f),
 120                     test_bit(SDF_WITHDRAW_RECOVERY, &f),
 121                     sdp->sd_log_error,
 122                     rwsem_is_locked(&sdp->sd_log_flush_lock),
 123                     sdp->sd_log_num_revoke,
 124                     atomic_read(&sdp->sd_log_in_flight),
 125                     atomic_read(&sdp->sd_log_blks_needed),
 126                     atomic_read(&sdp->sd_log_blks_free),
 127                     sdp->sd_log_flush_head,
 128                     sdp->sd_log_flush_tail,
 129                     sdp->sd_log_blks_reserved,
 130                     atomic_read(&sdp->sd_log_revokes_available));
 131        return s;
 132}
 133
 134static ssize_t fsname_show(struct gfs2_sbd *sdp, char *buf)
 135{
 136        return snprintf(buf, PAGE_SIZE, "%s\n", sdp->sd_fsname);
 137}
 138
 139static ssize_t uuid_show(struct gfs2_sbd *sdp, char *buf)
 140{
 141        struct super_block *s = sdp->sd_vfs;
 142
 143        buf[0] = '\0';
 144        if (uuid_is_null(&s->s_uuid))
 145                return 0;
 146        return snprintf(buf, PAGE_SIZE, "%pUB\n", &s->s_uuid);
 147}
 148
 149static ssize_t freeze_show(struct gfs2_sbd *sdp, char *buf)
 150{
 151        struct super_block *sb = sdp->sd_vfs;
 152        int frozen = (sb->s_writers.frozen == SB_UNFROZEN) ? 0 : 1;
 153
 154        return snprintf(buf, PAGE_SIZE, "%d\n", frozen);
 155}
 156
 157static ssize_t freeze_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
 158{
 159        int error, n;
 160
 161        error = kstrtoint(buf, 0, &n);
 162        if (error)
 163                return error;
 164
 165        if (!capable(CAP_SYS_ADMIN))
 166                return -EPERM;
 167
 168        switch (n) {
 169        case 0:
 170                error = thaw_super(sdp->sd_vfs);
 171                break;
 172        case 1:
 173                error = freeze_super(sdp->sd_vfs);
 174                break;
 175        default:
 176                return -EINVAL;
 177        }
 178
 179        if (error) {
 180                fs_warn(sdp, "freeze %d error %d\n", n, error);
 181                return error;
 182        }
 183
 184        return len;
 185}
 186
 187static ssize_t withdraw_show(struct gfs2_sbd *sdp, char *buf)
 188{
 189        unsigned int b = gfs2_withdrawn(sdp);
 190        return snprintf(buf, PAGE_SIZE, "%u\n", b);
 191}
 192
 193static ssize_t withdraw_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
 194{
 195        int error, val;
 196
 197        if (!capable(CAP_SYS_ADMIN))
 198                return -EPERM;
 199
 200        error = kstrtoint(buf, 0, &val);
 201        if (error)
 202                return error;
 203
 204        if (val != 1)
 205                return -EINVAL;
 206
 207        gfs2_lm(sdp, "withdrawing from cluster at user's request\n");
 208        gfs2_withdraw(sdp);
 209
 210        return len;
 211}
 212
 213static ssize_t statfs_sync_store(struct gfs2_sbd *sdp, const char *buf,
 214                                 size_t len)
 215{
 216        int error, val;
 217
 218        if (!capable(CAP_SYS_ADMIN))
 219                return -EPERM;
 220
 221        error = kstrtoint(buf, 0, &val);
 222        if (error)
 223                return error;
 224
 225        if (val != 1)
 226                return -EINVAL;
 227
 228        gfs2_statfs_sync(sdp->sd_vfs, 0);
 229        return len;
 230}
 231
 232static ssize_t quota_sync_store(struct gfs2_sbd *sdp, const char *buf,
 233                                size_t len)
 234{
 235        int error, val;
 236
 237        if (!capable(CAP_SYS_ADMIN))
 238                return -EPERM;
 239
 240        error = kstrtoint(buf, 0, &val);
 241        if (error)
 242                return error;
 243
 244        if (val != 1)
 245                return -EINVAL;
 246
 247        gfs2_quota_sync(sdp->sd_vfs, 0);
 248        return len;
 249}
 250
 251static ssize_t quota_refresh_user_store(struct gfs2_sbd *sdp, const char *buf,
 252                                        size_t len)
 253{
 254        struct kqid qid;
 255        int error;
 256        u32 id;
 257
 258        if (!capable(CAP_SYS_ADMIN))
 259                return -EPERM;
 260
 261        error = kstrtou32(buf, 0, &id);
 262        if (error)
 263                return error;
 264
 265        qid = make_kqid(current_user_ns(), USRQUOTA, id);
 266        if (!qid_valid(qid))
 267                return -EINVAL;
 268
 269        error = gfs2_quota_refresh(sdp, qid);
 270        return error ? error : len;
 271}
 272
 273static ssize_t quota_refresh_group_store(struct gfs2_sbd *sdp, const char *buf,
 274                                         size_t len)
 275{
 276        struct kqid qid;
 277        int error;
 278        u32 id;
 279
 280        if (!capable(CAP_SYS_ADMIN))
 281                return -EPERM;
 282
 283        error = kstrtou32(buf, 0, &id);
 284        if (error)
 285                return error;
 286
 287        qid = make_kqid(current_user_ns(), GRPQUOTA, id);
 288        if (!qid_valid(qid))
 289                return -EINVAL;
 290
 291        error = gfs2_quota_refresh(sdp, qid);
 292        return error ? error : len;
 293}
 294
 295static ssize_t demote_rq_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
 296{
 297        struct gfs2_glock *gl;
 298        const struct gfs2_glock_operations *glops;
 299        unsigned int glmode;
 300        unsigned int gltype;
 301        unsigned long long glnum;
 302        char mode[16];
 303        int rv;
 304
 305        if (!capable(CAP_SYS_ADMIN))
 306                return -EPERM;
 307
 308        rv = sscanf(buf, "%u:%llu %15s", &gltype, &glnum,
 309                    mode);
 310        if (rv != 3)
 311                return -EINVAL;
 312
 313        if (strcmp(mode, "EX") == 0)
 314                glmode = LM_ST_UNLOCKED;
 315        else if ((strcmp(mode, "CW") == 0) || (strcmp(mode, "DF") == 0))
 316                glmode = LM_ST_DEFERRED;
 317        else if ((strcmp(mode, "PR") == 0) || (strcmp(mode, "SH") == 0))
 318                glmode = LM_ST_SHARED;
 319        else
 320                return -EINVAL;
 321
 322        if (gltype > LM_TYPE_JOURNAL)
 323                return -EINVAL;
 324        if (gltype == LM_TYPE_NONDISK && glnum == GFS2_FREEZE_LOCK)
 325                glops = &gfs2_freeze_glops;
 326        else
 327                glops = gfs2_glops_list[gltype];
 328        if (glops == NULL)
 329                return -EINVAL;
 330        if (!test_and_set_bit(SDF_DEMOTE, &sdp->sd_flags))
 331                fs_info(sdp, "demote interface used\n");
 332        rv = gfs2_glock_get(sdp, glnum, glops, 0, &gl);
 333        if (rv)
 334                return rv;
 335        gfs2_glock_cb(gl, glmode);
 336        gfs2_glock_put(gl);
 337        return len;
 338}
 339
 340
 341#define GFS2_ATTR(name, mode, show, store) \
 342static struct gfs2_attr gfs2_attr_##name = __ATTR(name, mode, show, store)
 343
 344GFS2_ATTR(id,                  0444, id_show,       NULL);
 345GFS2_ATTR(fsname,              0444, fsname_show,   NULL);
 346GFS2_ATTR(uuid,                0444, uuid_show,     NULL);
 347GFS2_ATTR(freeze,              0644, freeze_show,   freeze_store);
 348GFS2_ATTR(withdraw,            0644, withdraw_show, withdraw_store);
 349GFS2_ATTR(statfs_sync,         0200, NULL,          statfs_sync_store);
 350GFS2_ATTR(quota_sync,          0200, NULL,          quota_sync_store);
 351GFS2_ATTR(quota_refresh_user,  0200, NULL,          quota_refresh_user_store);
 352GFS2_ATTR(quota_refresh_group, 0200, NULL,          quota_refresh_group_store);
 353GFS2_ATTR(demote_rq,           0200, NULL,          demote_rq_store);
 354GFS2_ATTR(status,              0400, status_show,   NULL);
 355
 356static struct attribute *gfs2_attrs[] = {
 357        &gfs2_attr_id.attr,
 358        &gfs2_attr_fsname.attr,
 359        &gfs2_attr_uuid.attr,
 360        &gfs2_attr_freeze.attr,
 361        &gfs2_attr_withdraw.attr,
 362        &gfs2_attr_statfs_sync.attr,
 363        &gfs2_attr_quota_sync.attr,
 364        &gfs2_attr_quota_refresh_user.attr,
 365        &gfs2_attr_quota_refresh_group.attr,
 366        &gfs2_attr_demote_rq.attr,
 367        &gfs2_attr_status.attr,
 368        NULL,
 369};
 370
 371static void gfs2_sbd_release(struct kobject *kobj)
 372{
 373        struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
 374
 375        complete(&sdp->sd_kobj_unregister);
 376}
 377
 378static struct kobj_type gfs2_ktype = {
 379        .release = gfs2_sbd_release,
 380        .default_attrs = gfs2_attrs,
 381        .sysfs_ops     = &gfs2_attr_ops,
 382};
 383
 384
 385/*
 386 * lock_module. Originally from lock_dlm
 387 */
 388
 389static ssize_t proto_name_show(struct gfs2_sbd *sdp, char *buf)
 390{
 391        const struct lm_lockops *ops = sdp->sd_lockstruct.ls_ops;
 392        return sprintf(buf, "%s\n", ops->lm_proto_name);
 393}
 394
 395static ssize_t block_show(struct gfs2_sbd *sdp, char *buf)
 396{
 397        struct lm_lockstruct *ls = &sdp->sd_lockstruct;
 398        ssize_t ret;
 399        int val = 0;
 400
 401        if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))
 402                val = 1;
 403        ret = sprintf(buf, "%d\n", val);
 404        return ret;
 405}
 406
 407static ssize_t block_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
 408{
 409        struct lm_lockstruct *ls = &sdp->sd_lockstruct;
 410        int ret, val;
 411
 412        ret = kstrtoint(buf, 0, &val);
 413        if (ret)
 414                return ret;
 415
 416        if (val == 1)
 417                set_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
 418        else if (val == 0) {
 419                clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
 420                smp_mb__after_atomic();
 421                gfs2_glock_thaw(sdp);
 422        } else {
 423                return -EINVAL;
 424        }
 425        return len;
 426}
 427
 428static ssize_t wdack_show(struct gfs2_sbd *sdp, char *buf)
 429{
 430        int val = completion_done(&sdp->sd_wdack) ? 1 : 0;
 431
 432        return sprintf(buf, "%d\n", val);
 433}
 434
 435static ssize_t wdack_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
 436{
 437        int ret, val;
 438
 439        ret = kstrtoint(buf, 0, &val);
 440        if (ret)
 441                return ret;
 442
 443        if ((val == 1) &&
 444            !strcmp(sdp->sd_lockstruct.ls_ops->lm_proto_name, "lock_dlm"))
 445                complete(&sdp->sd_wdack);
 446        else
 447                return -EINVAL;
 448        return len;
 449}
 450
 451static ssize_t lkfirst_show(struct gfs2_sbd *sdp, char *buf)
 452{
 453        struct lm_lockstruct *ls = &sdp->sd_lockstruct;
 454        return sprintf(buf, "%d\n", ls->ls_first);
 455}
 456
 457static ssize_t lkfirst_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
 458{
 459        unsigned first;
 460        int rv;
 461
 462        rv = sscanf(buf, "%u", &first);
 463        if (rv != 1 || first > 1)
 464                return -EINVAL;
 465        rv = wait_for_completion_killable(&sdp->sd_locking_init);
 466        if (rv)
 467                return rv;
 468        spin_lock(&sdp->sd_jindex_spin);
 469        rv = -EBUSY;
 470        if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
 471                goto out;
 472        rv = -EINVAL;
 473        if (sdp->sd_args.ar_spectator)
 474                goto out;
 475        if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
 476                goto out;
 477        sdp->sd_lockstruct.ls_first = first;
 478        rv = 0;
 479out:
 480        spin_unlock(&sdp->sd_jindex_spin);
 481        return rv ? rv : len;
 482}
 483
 484static ssize_t first_done_show(struct gfs2_sbd *sdp, char *buf)
 485{
 486        struct lm_lockstruct *ls = &sdp->sd_lockstruct;
 487        return sprintf(buf, "%d\n", !!test_bit(DFL_FIRST_MOUNT_DONE, &ls->ls_recover_flags));
 488}
 489
 490int gfs2_recover_set(struct gfs2_sbd *sdp, unsigned jid)
 491{
 492        struct gfs2_jdesc *jd;
 493        int rv;
 494
 495        /* Wait for our primary journal to be initialized */
 496        wait_for_completion(&sdp->sd_journal_ready);
 497
 498        spin_lock(&sdp->sd_jindex_spin);
 499        rv = -EBUSY;
 500        /**
 501         * If we're a spectator, we use journal0, but it's not really ours.
 502         * So we need to wait for its recovery too. If we skip it we'd never
 503         * queue work to the recovery workqueue, and so its completion would
 504         * never clear the DFL_BLOCK_LOCKS flag, so all our locks would
 505         * permanently stop working.
 506         */
 507        if (!sdp->sd_jdesc)
 508                goto out;
 509        if (sdp->sd_jdesc->jd_jid == jid && !sdp->sd_args.ar_spectator)
 510                goto out;
 511        rv = -ENOENT;
 512        list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
 513                if (jd->jd_jid != jid && !sdp->sd_args.ar_spectator)
 514                        continue;
 515                rv = gfs2_recover_journal(jd, false);
 516                break;
 517        }
 518out:
 519        spin_unlock(&sdp->sd_jindex_spin);
 520        return rv;
 521}
 522
 523static ssize_t recover_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
 524{
 525        unsigned jid;
 526        int rv;
 527
 528        rv = sscanf(buf, "%u", &jid);
 529        if (rv != 1)
 530                return -EINVAL;
 531
 532        if (test_bit(SDF_NORECOVERY, &sdp->sd_flags)) {
 533                rv = -ESHUTDOWN;
 534                goto out;
 535        }
 536
 537        rv = gfs2_recover_set(sdp, jid);
 538out:
 539        return rv ? rv : len;
 540}
 541
 542static ssize_t recover_done_show(struct gfs2_sbd *sdp, char *buf)
 543{
 544        struct lm_lockstruct *ls = &sdp->sd_lockstruct;
 545        return sprintf(buf, "%d\n", ls->ls_recover_jid_done);
 546}
 547
 548static ssize_t recover_status_show(struct gfs2_sbd *sdp, char *buf)
 549{
 550        struct lm_lockstruct *ls = &sdp->sd_lockstruct;
 551        return sprintf(buf, "%d\n", ls->ls_recover_jid_status);
 552}
 553
 554static ssize_t jid_show(struct gfs2_sbd *sdp, char *buf)
 555{
 556        return sprintf(buf, "%d\n", sdp->sd_lockstruct.ls_jid);
 557}
 558
 559static ssize_t jid_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
 560{
 561        int jid;
 562        int rv;
 563
 564        rv = sscanf(buf, "%d", &jid);
 565        if (rv != 1)
 566                return -EINVAL;
 567        rv = wait_for_completion_killable(&sdp->sd_locking_init);
 568        if (rv)
 569                return rv;
 570        spin_lock(&sdp->sd_jindex_spin);
 571        rv = -EINVAL;
 572        if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
 573                goto out;
 574        rv = -EBUSY;
 575        if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
 576                goto out;
 577        rv = 0;
 578        if (sdp->sd_args.ar_spectator && jid > 0)
 579                rv = jid = -EINVAL;
 580        sdp->sd_lockstruct.ls_jid = jid;
 581        clear_bit(SDF_NOJOURNALID, &sdp->sd_flags);
 582        smp_mb__after_atomic();
 583        wake_up_bit(&sdp->sd_flags, SDF_NOJOURNALID);
 584out:
 585        spin_unlock(&sdp->sd_jindex_spin);
 586        return rv ? rv : len;
 587}
 588
 589#define GDLM_ATTR(_name,_mode,_show,_store) \
 590static struct gfs2_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
 591
 592GDLM_ATTR(proto_name,           0444, proto_name_show,          NULL);
 593GDLM_ATTR(block,                0644, block_show,               block_store);
 594GDLM_ATTR(withdraw,             0644, wdack_show,               wdack_store);
 595GDLM_ATTR(jid,                  0644, jid_show,                 jid_store);
 596GDLM_ATTR(first,                0644, lkfirst_show,             lkfirst_store);
 597GDLM_ATTR(first_done,           0444, first_done_show,          NULL);
 598GDLM_ATTR(recover,              0600, NULL,                     recover_store);
 599GDLM_ATTR(recover_done,         0444, recover_done_show,        NULL);
 600GDLM_ATTR(recover_status,       0444, recover_status_show,      NULL);
 601
 602static struct attribute *lock_module_attrs[] = {
 603        &gdlm_attr_proto_name.attr,
 604        &gdlm_attr_block.attr,
 605        &gdlm_attr_withdraw.attr,
 606        &gdlm_attr_jid.attr,
 607        &gdlm_attr_first.attr,
 608        &gdlm_attr_first_done.attr,
 609        &gdlm_attr_recover.attr,
 610        &gdlm_attr_recover_done.attr,
 611        &gdlm_attr_recover_status.attr,
 612        NULL,
 613};
 614
 615/*
 616 * get and set struct gfs2_tune fields
 617 */
 618
 619static ssize_t quota_scale_show(struct gfs2_sbd *sdp, char *buf)
 620{
 621        return snprintf(buf, PAGE_SIZE, "%u %u\n",
 622                        sdp->sd_tune.gt_quota_scale_num,
 623                        sdp->sd_tune.gt_quota_scale_den);
 624}
 625
 626static ssize_t quota_scale_store(struct gfs2_sbd *sdp, const char *buf,
 627                                 size_t len)
 628{
 629        struct gfs2_tune *gt = &sdp->sd_tune;
 630        unsigned int x, y;
 631
 632        if (!capable(CAP_SYS_ADMIN))
 633                return -EPERM;
 634
 635        if (sscanf(buf, "%u %u", &x, &y) != 2 || !y)
 636                return -EINVAL;
 637
 638        spin_lock(&gt->gt_spin);
 639        gt->gt_quota_scale_num = x;
 640        gt->gt_quota_scale_den = y;
 641        spin_unlock(&gt->gt_spin);
 642        return len;
 643}
 644
 645static ssize_t tune_set(struct gfs2_sbd *sdp, unsigned int *field,
 646                        int check_zero, const char *buf, size_t len)
 647{
 648        struct gfs2_tune *gt = &sdp->sd_tune;
 649        unsigned int x;
 650        int error;
 651
 652        if (!capable(CAP_SYS_ADMIN))
 653                return -EPERM;
 654
 655        error = kstrtouint(buf, 0, &x);
 656        if (error)
 657                return error;
 658
 659        if (check_zero && !x)
 660                return -EINVAL;
 661
 662        spin_lock(&gt->gt_spin);
 663        *field = x;
 664        spin_unlock(&gt->gt_spin);
 665        return len;
 666}
 667
 668#define TUNE_ATTR_3(name, show, store)                                        \
 669static struct gfs2_attr tune_attr_##name = __ATTR(name, 0644, show, store)
 670
 671#define TUNE_ATTR_2(name, store)                                              \
 672static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf)                   \
 673{                                                                             \
 674        return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name);      \
 675}                                                                             \
 676TUNE_ATTR_3(name, name##_show, store)
 677
 678#define TUNE_ATTR(name, check_zero)                                           \
 679static ssize_t name##_store(struct gfs2_sbd *sdp, const char *buf, size_t len)\
 680{                                                                             \
 681        return tune_set(sdp, &sdp->sd_tune.gt_##name, check_zero, buf, len);  \
 682}                                                                             \
 683TUNE_ATTR_2(name, name##_store)
 684
 685TUNE_ATTR(quota_warn_period, 0);
 686TUNE_ATTR(quota_quantum, 0);
 687TUNE_ATTR(max_readahead, 0);
 688TUNE_ATTR(complain_secs, 0);
 689TUNE_ATTR(statfs_slow, 0);
 690TUNE_ATTR(new_files_jdata, 0);
 691TUNE_ATTR(statfs_quantum, 1);
 692TUNE_ATTR_3(quota_scale, quota_scale_show, quota_scale_store);
 693
 694static struct attribute *tune_attrs[] = {
 695        &tune_attr_quota_warn_period.attr,
 696        &tune_attr_quota_quantum.attr,
 697        &tune_attr_max_readahead.attr,
 698        &tune_attr_complain_secs.attr,
 699        &tune_attr_statfs_slow.attr,
 700        &tune_attr_statfs_quantum.attr,
 701        &tune_attr_quota_scale.attr,
 702        &tune_attr_new_files_jdata.attr,
 703        NULL,
 704};
 705
 706static const struct attribute_group tune_group = {
 707        .name = "tune",
 708        .attrs = tune_attrs,
 709};
 710
 711static const struct attribute_group lock_module_group = {
 712        .name = "lock_module",
 713        .attrs = lock_module_attrs,
 714};
 715
 716int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
 717{
 718        struct super_block *sb = sdp->sd_vfs;
 719        int error;
 720        char ro[20];
 721        char spectator[20];
 722        char *envp[] = { ro, spectator, NULL };
 723
 724        sprintf(ro, "RDONLY=%d", sb_rdonly(sb));
 725        sprintf(spectator, "SPECTATOR=%d", sdp->sd_args.ar_spectator ? 1 : 0);
 726
 727        init_completion(&sdp->sd_kobj_unregister);
 728        sdp->sd_kobj.kset = gfs2_kset;
 729        error = kobject_init_and_add(&sdp->sd_kobj, &gfs2_ktype, NULL,
 730                                     "%s", sdp->sd_table_name);
 731        if (error)
 732                goto fail_reg;
 733
 734        error = sysfs_create_group(&sdp->sd_kobj, &tune_group);
 735        if (error)
 736                goto fail_reg;
 737
 738        error = sysfs_create_group(&sdp->sd_kobj, &lock_module_group);
 739        if (error)
 740                goto fail_tune;
 741
 742        error = sysfs_create_link(&sdp->sd_kobj,
 743                                  &disk_to_dev(sb->s_bdev->bd_disk)->kobj,
 744                                  "device");
 745        if (error)
 746                goto fail_lock_module;
 747
 748        kobject_uevent_env(&sdp->sd_kobj, KOBJ_ADD, envp);
 749        return 0;
 750
 751fail_lock_module:
 752        sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
 753fail_tune:
 754        sysfs_remove_group(&sdp->sd_kobj, &tune_group);
 755fail_reg:
 756        fs_err(sdp, "error %d adding sysfs files\n", error);
 757        kobject_put(&sdp->sd_kobj);
 758        wait_for_completion(&sdp->sd_kobj_unregister);
 759        sb->s_fs_info = NULL;
 760        return error;
 761}
 762
 763void gfs2_sys_fs_del(struct gfs2_sbd *sdp)
 764{
 765        sysfs_remove_link(&sdp->sd_kobj, "device");
 766        sysfs_remove_group(&sdp->sd_kobj, &tune_group);
 767        sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
 768        kobject_put(&sdp->sd_kobj);
 769        wait_for_completion(&sdp->sd_kobj_unregister);
 770}
 771
 772static int gfs2_uevent(struct kset *kset, struct kobject *kobj,
 773                       struct kobj_uevent_env *env)
 774{
 775        struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
 776        struct super_block *s = sdp->sd_vfs;
 777
 778        add_uevent_var(env, "LOCKTABLE=%s", sdp->sd_table_name);
 779        add_uevent_var(env, "LOCKPROTO=%s", sdp->sd_proto_name);
 780        if (!test_bit(SDF_NOJOURNALID, &sdp->sd_flags))
 781                add_uevent_var(env, "JOURNALID=%d", sdp->sd_lockstruct.ls_jid);
 782        if (!uuid_is_null(&s->s_uuid))
 783                add_uevent_var(env, "UUID=%pUB", &s->s_uuid);
 784        return 0;
 785}
 786
 787static const struct kset_uevent_ops gfs2_uevent_ops = {
 788        .uevent = gfs2_uevent,
 789};
 790
 791int gfs2_sys_init(void)
 792{
 793        gfs2_kset = kset_create_and_add("gfs2", &gfs2_uevent_ops, fs_kobj);
 794        if (!gfs2_kset)
 795                return -ENOMEM;
 796        return 0;
 797}
 798
 799void gfs2_sys_uninit(void)
 800{
 801        kset_unregister(gfs2_kset);
 802}
 803
 804