sys.c 17.6 KB
Newer Older
David Teigland's avatar
David Teigland committed
1 2
/*
 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3
 * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
David Teigland's avatar
David Teigland committed
4 5 6
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
7
 * of the GNU General Public License version 2.
David Teigland's avatar
David Teigland committed
8 9
 */

10 11
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

David Teigland's avatar
David Teigland committed
12
#include <linux/sched.h>
13
#include <linux/cred.h>
David Teigland's avatar
David Teigland committed
14 15 16 17 18
#include <linux/spinlock.h>
#include <linux/completion.h>
#include <linux/buffer_head.h>
#include <linux/module.h>
#include <linux/kobject.h>
19
#include <linux/uaccess.h>
20
#include <linux/gfs2_ondisk.h>
21
#include <linux/genhd.h>
David Teigland's avatar
David Teigland committed
22 23

#include "gfs2.h"
24
#include "incore.h"
David Teigland's avatar
David Teigland committed
25 26 27 28
#include "sys.h"
#include "super.h"
#include "glock.h"
#include "quota.h"
29
#include "util.h"
30
#include "glops.h"
31
#include "recovery.h"
David Teigland's avatar
David Teigland committed
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
struct gfs2_attr {
	struct attribute attr;
	ssize_t (*show)(struct gfs2_sbd *, char *);
	ssize_t (*store)(struct gfs2_sbd *, const char *, size_t);
};

static ssize_t gfs2_attr_show(struct kobject *kobj, struct attribute *attr,
			      char *buf)
{
	struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
	struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
	return a->show ? a->show(sdp, buf) : 0;
}

static ssize_t gfs2_attr_store(struct kobject *kobj, struct attribute *attr,
			       const char *buf, size_t len)
{
	struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
	struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
	return a->store ? a->store(sdp, buf, len) : len;
}

55
static const struct sysfs_ops gfs2_attr_ops = {
56 57 58 59 60 61 62
	.show  = gfs2_attr_show,
	.store = gfs2_attr_store,
};


static struct kset *gfs2_kset;

David Teigland's avatar
David Teigland committed
63 64
static ssize_t id_show(struct gfs2_sbd *sdp, char *buf)
{
65 66
	return snprintf(buf, PAGE_SIZE, "%u:%u\n",
			MAJOR(sdp->sd_vfs->s_dev), MINOR(sdp->sd_vfs->s_dev));
David Teigland's avatar
David Teigland committed
67 68 69 70
}

static ssize_t fsname_show(struct gfs2_sbd *sdp, char *buf)
{
71
	return snprintf(buf, PAGE_SIZE, "%s\n", sdp->sd_fsname);
David Teigland's avatar
David Teigland committed
72 73
}

74 75 76 77 78 79 80 81 82 83 84 85 86
static int gfs2_uuid_valid(const u8 *uuid)
{
	int i;

	for (i = 0; i < 16; i++) {
		if (uuid[i])
			return 1;
	}
	return 0;
}

static ssize_t uuid_show(struct gfs2_sbd *sdp, char *buf)
{
87 88
	struct super_block *s = sdp->sd_vfs;
	const u8 *uuid = s->s_uuid;
89 90 91
	buf[0] = '\0';
	if (!gfs2_uuid_valid(uuid))
		return 0;
92
	return snprintf(buf, PAGE_SIZE, "%pUB\n", uuid);
93 94
}

David Teigland's avatar
David Teigland committed
95 96
static ssize_t freeze_show(struct gfs2_sbd *sdp, char *buf)
{
Steven Whitehouse's avatar
Steven Whitehouse committed
97 98
	struct super_block *sb = sdp->sd_vfs;
	int frozen = (sb->s_writers.frozen == SB_UNFROZEN) ? 0 : 1;
David Teigland's avatar
David Teigland committed
99

alex chen's avatar
alex chen committed
100
	return snprintf(buf, PAGE_SIZE, "%d\n", frozen);
David Teigland's avatar
David Teigland committed
101 102 103 104
}

static ssize_t freeze_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
{
105 106 107 108 109
	int error, n;

	error = kstrtoint(buf, 0, &n);
	if (error)
		return error;
David Teigland's avatar
David Teigland committed
110 111

	if (!capable(CAP_SYS_ADMIN))
112
		return -EPERM;
David Teigland's avatar
David Teigland committed
113 114 115

	switch (n) {
	case 0:
Steven Whitehouse's avatar
Steven Whitehouse committed
116
		error = thaw_super(sdp->sd_vfs);
David Teigland's avatar
David Teigland committed
117 118
		break;
	case 1:
Steven Whitehouse's avatar
Steven Whitehouse committed
119
		error = freeze_super(sdp->sd_vfs);
David Teigland's avatar
David Teigland committed
120 121
		break;
	default:
Steven Whitehouse's avatar
Steven Whitehouse committed
122
		return -EINVAL;
David Teigland's avatar
David Teigland committed
123 124
	}

Steven Whitehouse's avatar
Steven Whitehouse committed
125
	if (error) {
David Teigland's avatar
David Teigland committed
126
		fs_warn(sdp, "freeze %d error %d", n, error);
Steven Whitehouse's avatar
Steven Whitehouse committed
127 128
		return error;
	}
David Teigland's avatar
David Teigland committed
129

Steven Whitehouse's avatar
Steven Whitehouse committed
130
	return len;
David Teigland's avatar
David Teigland committed
131 132 133 134 135
}

static ssize_t withdraw_show(struct gfs2_sbd *sdp, char *buf)
{
	unsigned int b = test_bit(SDF_SHUTDOWN, &sdp->sd_flags);
136
	return snprintf(buf, PAGE_SIZE, "%u\n", b);
David Teigland's avatar
David Teigland committed
137 138 139 140
}

static ssize_t withdraw_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
{
141 142
	int error, val;

David Teigland's avatar
David Teigland committed
143
	if (!capable(CAP_SYS_ADMIN))
144
		return -EPERM;
David Teigland's avatar
David Teigland committed
145

146 147 148 149 150
	error = kstrtoint(buf, 0, &val);
	if (error)
		return error;

	if (val != 1)
David Teigland's avatar
David Teigland committed
151 152
		return -EINVAL;

153 154
	gfs2_lm_withdraw(sdp, "withdrawing from cluster at user's request\n");

David Teigland's avatar
David Teigland committed
155 156 157 158 159 160
	return len;
}

static ssize_t statfs_sync_store(struct gfs2_sbd *sdp, const char *buf,
				 size_t len)
{
161 162
	int error, val;

David Teigland's avatar
David Teigland committed
163
	if (!capable(CAP_SYS_ADMIN))
164
		return -EPERM;
David Teigland's avatar
David Teigland committed
165

166 167 168 169 170
	error = kstrtoint(buf, 0, &val);
	if (error)
		return error;

	if (val != 1)
David Teigland's avatar
David Teigland committed
171 172
		return -EINVAL;

173
	gfs2_statfs_sync(sdp->sd_vfs, 0);
David Teigland's avatar
David Teigland committed
174 175 176 177 178 179
	return len;
}

static ssize_t quota_sync_store(struct gfs2_sbd *sdp, const char *buf,
				size_t len)
{
180 181
	int error, val;

David Teigland's avatar
David Teigland committed
182
	if (!capable(CAP_SYS_ADMIN))
183
		return -EPERM;
David Teigland's avatar
David Teigland committed
184

185 186 187 188 189
	error = kstrtoint(buf, 0, &val);
	if (error)
		return error;

	if (val != 1)
David Teigland's avatar
David Teigland committed
190 191
		return -EINVAL;

192
	gfs2_quota_sync(sdp->sd_vfs, 0);
David Teigland's avatar
David Teigland committed
193 194 195 196 197 198
	return len;
}

static ssize_t quota_refresh_user_store(struct gfs2_sbd *sdp, const char *buf,
					size_t len)
{
199
	struct kqid qid;
200
	int error;
201
	u32 id;
David Teigland's avatar
David Teigland committed
202 203

	if (!capable(CAP_SYS_ADMIN))
204
		return -EPERM;
David Teigland's avatar
David Teigland committed
205

206 207 208
	error = kstrtou32(buf, 0, &id);
	if (error)
		return error;
David Teigland's avatar
David Teigland committed
209

210 211 212 213 214
	qid = make_kqid(current_user_ns(), USRQUOTA, id);
	if (!qid_valid(qid))
		return -EINVAL;

	error = gfs2_quota_refresh(sdp, qid);
215
	return error ? error : len;
David Teigland's avatar
David Teigland committed
216 217 218 219 220
}

static ssize_t quota_refresh_group_store(struct gfs2_sbd *sdp, const char *buf,
					 size_t len)
{
221
	struct kqid qid;
222
	int error;
223
	u32 id;
David Teigland's avatar
David Teigland committed
224 225

	if (!capable(CAP_SYS_ADMIN))
226
		return -EPERM;
David Teigland's avatar
David Teigland committed
227

228 229 230
	error = kstrtou32(buf, 0, &id);
	if (error)
		return error;
David Teigland's avatar
David Teigland committed
231

232 233 234 235 236
	qid = make_kqid(current_user_ns(), GRPQUOTA, id);
	if (!qid_valid(qid))
		return -EINVAL;

	error = gfs2_quota_refresh(sdp, qid);
237
	return error ? error : len;
David Teigland's avatar
David Teigland committed
238 239
}

240 241 242 243 244 245 246 247 248 249 250
static ssize_t demote_rq_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
{
	struct gfs2_glock *gl;
	const struct gfs2_glock_operations *glops;
	unsigned int glmode;
	unsigned int gltype;
	unsigned long long glnum;
	char mode[16];
	int rv;

	if (!capable(CAP_SYS_ADMIN))
251
		return -EPERM;
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

	rv = sscanf(buf, "%u:%llu %15s", &gltype, &glnum,
		    mode);
	if (rv != 3)
		return -EINVAL;

	if (strcmp(mode, "EX") == 0)
		glmode = LM_ST_UNLOCKED;
	else if ((strcmp(mode, "CW") == 0) || (strcmp(mode, "DF") == 0))
		glmode = LM_ST_DEFERRED;
	else if ((strcmp(mode, "PR") == 0) || (strcmp(mode, "SH") == 0))
		glmode = LM_ST_SHARED;
	else
		return -EINVAL;

	if (gltype > LM_TYPE_JOURNAL)
		return -EINVAL;
269 270
	if (gltype == LM_TYPE_NONDISK && glnum == GFS2_FREEZE_LOCK)
		glops = &gfs2_freeze_glops;
271 272
	else
		glops = gfs2_glops_list[gltype];
273 274
	if (glops == NULL)
		return -EINVAL;
Steven Whitehouse's avatar
Steven Whitehouse committed
275
	if (!test_and_set_bit(SDF_DEMOTE, &sdp->sd_flags))
276
		fs_info(sdp, "demote interface used\n");
277 278 279 280 281 282 283 284
	rv = gfs2_glock_get(sdp, glnum, glops, 0, &gl);
	if (rv)
		return rv;
	gfs2_glock_cb(gl, glmode);
	gfs2_glock_put(gl);
	return len;
}

David Teigland's avatar
David Teigland committed
285 286 287 288 289 290

#define GFS2_ATTR(name, mode, show, store) \
static struct gfs2_attr gfs2_attr_##name = __ATTR(name, mode, show, store)

GFS2_ATTR(id,                  0444, id_show,       NULL);
GFS2_ATTR(fsname,              0444, fsname_show,   NULL);
291
GFS2_ATTR(uuid,                0444, uuid_show,     NULL);
David Teigland's avatar
David Teigland committed
292 293 294 295 296 297
GFS2_ATTR(freeze,              0644, freeze_show,   freeze_store);
GFS2_ATTR(withdraw,            0644, withdraw_show, withdraw_store);
GFS2_ATTR(statfs_sync,         0200, NULL,          statfs_sync_store);
GFS2_ATTR(quota_sync,          0200, NULL,          quota_sync_store);
GFS2_ATTR(quota_refresh_user,  0200, NULL,          quota_refresh_user_store);
GFS2_ATTR(quota_refresh_group, 0200, NULL,          quota_refresh_group_store);
298
GFS2_ATTR(demote_rq,           0200, NULL,	    demote_rq_store);
David Teigland's avatar
David Teigland committed
299 300 301 302

static struct attribute *gfs2_attrs[] = {
	&gfs2_attr_id.attr,
	&gfs2_attr_fsname.attr,
303
	&gfs2_attr_uuid.attr,
David Teigland's avatar
David Teigland committed
304 305 306 307 308 309
	&gfs2_attr_freeze.attr,
	&gfs2_attr_withdraw.attr,
	&gfs2_attr_statfs_sync.attr,
	&gfs2_attr_quota_sync.attr,
	&gfs2_attr_quota_refresh_user.attr,
	&gfs2_attr_quota_refresh_group.attr,
310
	&gfs2_attr_demote_rq.attr,
David Teigland's avatar
David Teigland committed
311 312 313
	NULL,
};

314 315 316 317 318 319 320
static void gfs2_sbd_release(struct kobject *kobj)
{
	struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);

	kfree(sdp);
}

David Teigland's avatar
David Teigland committed
321
static struct kobj_type gfs2_ktype = {
322
	.release = gfs2_sbd_release,
David Teigland's avatar
David Teigland committed
323 324 325 326
	.default_attrs = gfs2_attrs,
	.sysfs_ops     = &gfs2_attr_ops,
};

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

/*
 * lock_module. Originally from lock_dlm
 */

static ssize_t proto_name_show(struct gfs2_sbd *sdp, char *buf)
{
	const struct lm_lockops *ops = sdp->sd_lockstruct.ls_ops;
	return sprintf(buf, "%s\n", ops->lm_proto_name);
}

static ssize_t block_show(struct gfs2_sbd *sdp, char *buf)
{
	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
	ssize_t ret;
	int val = 0;

344
	if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))
345 346 347 348 349 350 351 352
		val = 1;
	ret = sprintf(buf, "%d\n", val);
	return ret;
}

static ssize_t block_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
{
	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
353
	int ret, val;
354

355 356 357
	ret = kstrtoint(buf, 0, &val);
	if (ret)
		return ret;
358 359

	if (val == 1)
360
		set_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
361
	else if (val == 0) {
362
		clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
363
		smp_mb__after_atomic();
364 365
		gfs2_glock_thaw(sdp);
	} else {
366
		return -EINVAL;
367
	}
368
	return len;
369 370
}

371 372 373 374 375 376 377 378 379
static ssize_t wdack_show(struct gfs2_sbd *sdp, char *buf)
{
	int val = completion_done(&sdp->sd_wdack) ? 1 : 0;

	return sprintf(buf, "%d\n", val);
}

static ssize_t wdack_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
{
380
	int ret, val;
381

382 383 384
	ret = kstrtoint(buf, 0, &val);
	if (ret)
		return ret;
385 386 387 388 389

	if ((val == 1) &&
	    !strcmp(sdp->sd_lockstruct.ls_ops->lm_proto_name, "lock_dlm"))
		complete(&sdp->sd_wdack);
	else
390 391
		return -EINVAL;
	return len;
392 393
}

394 395 396 397 398 399
static ssize_t lkfirst_show(struct gfs2_sbd *sdp, char *buf)
{
	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
	return sprintf(buf, "%d\n", ls->ls_first);
}

400 401 402 403 404 405 406 407
static ssize_t lkfirst_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
{
	unsigned first;
	int rv;

	rv = sscanf(buf, "%u", &first);
	if (rv != 1 || first > 1)
		return -EINVAL;
408 409 410
	rv = wait_for_completion_killable(&sdp->sd_locking_init);
	if (rv)
		return rv;
411 412 413 414 415 416 417 418 419
	spin_lock(&sdp->sd_jindex_spin);
	rv = -EBUSY;
	if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
		goto out;
	rv = -EINVAL;
	if (sdp->sd_args.ar_spectator)
		goto out;
	if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
		goto out;
420 421
	sdp->sd_lockstruct.ls_first = first;
	rv = 0;
422 423 424 425 426
out:
        spin_unlock(&sdp->sd_jindex_spin);
        return rv ? rv : len;
}

427 428 429
static ssize_t first_done_show(struct gfs2_sbd *sdp, char *buf)
{
	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
430
	return sprintf(buf, "%d\n", !!test_bit(DFL_FIRST_MOUNT_DONE, &ls->ls_recover_flags));
431 432
}

433
int gfs2_recover_set(struct gfs2_sbd *sdp, unsigned jid)
434 435
{
	struct gfs2_jdesc *jd;
436 437
	int rv;

438 439 440
	/* Wait for our primary journal to be initialized */
	wait_for_completion(&sdp->sd_journal_ready);

441
	spin_lock(&sdp->sd_jindex_spin);
442 443 444 445
	rv = -EBUSY;
	if (sdp->sd_jdesc->jd_jid == jid)
		goto out;
	rv = -ENOENT;
446 447 448
	list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
		if (jd->jd_jid != jid)
			continue;
449
		rv = gfs2_recover_journal(jd, false);
450 451
		break;
	}
452
out:
453
	spin_unlock(&sdp->sd_jindex_spin);
454 455 456 457 458 459 460 461 462 463 464 465
	return rv;
}

static ssize_t recover_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
{
	unsigned jid;
	int rv;

	rv = sscanf(buf, "%u", &jid);
	if (rv != 1)
		return -EINVAL;

466 467 468 469
	if (test_bit(SDF_NORECOVERY, &sdp->sd_flags)) {
		rv = -ESHUTDOWN;
		goto out;
	}
470

471 472
	rv = gfs2_recover_set(sdp, jid);
out:
473
	return rv ? rv : len;
474 475 476 477 478 479 480 481 482 483 484 485 486 487
}

static ssize_t recover_done_show(struct gfs2_sbd *sdp, char *buf)
{
	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
	return sprintf(buf, "%d\n", ls->ls_recover_jid_done);
}

static ssize_t recover_status_show(struct gfs2_sbd *sdp, char *buf)
{
	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
	return sprintf(buf, "%d\n", ls->ls_recover_jid_status);
}

488 489
static ssize_t jid_show(struct gfs2_sbd *sdp, char *buf)
{
490
	return sprintf(buf, "%d\n", sdp->sd_lockstruct.ls_jid);
491 492
}

493 494
static ssize_t jid_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
{
495
        int jid;
496 497
	int rv;

498
	rv = sscanf(buf, "%d", &jid);
499 500
	if (rv != 1)
		return -EINVAL;
501 502 503
	rv = wait_for_completion_killable(&sdp->sd_locking_init);
	if (rv)
		return rv;
504 505 506 507 508
	spin_lock(&sdp->sd_jindex_spin);
	rv = -EINVAL;
	if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
		goto out;
	rv = -EBUSY;
509
	if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
510
		goto out;
511 512 513
	rv = 0;
	if (sdp->sd_args.ar_spectator && jid > 0)
		rv = jid = -EINVAL;
514
	sdp->sd_lockstruct.ls_jid = jid;
515
	clear_bit(SDF_NOJOURNALID, &sdp->sd_flags);
516
	smp_mb__after_atomic();
517 518 519 520 521 522
	wake_up_bit(&sdp->sd_flags, SDF_NOJOURNALID);
out:
	spin_unlock(&sdp->sd_jindex_spin);
	return rv ? rv : len;
}

523
#define GDLM_ATTR(_name,_mode,_show,_store) \
524
static struct gfs2_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
525

526 527
GDLM_ATTR(proto_name,		0444, proto_name_show,		NULL);
GDLM_ATTR(block,		0644, block_show,		block_store);
528
GDLM_ATTR(withdraw,		0644, wdack_show,		wdack_store);
529 530
GDLM_ATTR(jid,			0644, jid_show,			jid_store);
GDLM_ATTR(first,		0644, lkfirst_show,		lkfirst_store);
531 532 533 534
GDLM_ATTR(first_done,		0444, first_done_show,		NULL);
GDLM_ATTR(recover,		0600, NULL,			recover_store);
GDLM_ATTR(recover_done,		0444, recover_done_show,	NULL);
GDLM_ATTR(recover_status,	0444, recover_status_show,	NULL);
535 536 537 538 539

static struct attribute *lock_module_attrs[] = {
	&gdlm_attr_proto_name.attr,
	&gdlm_attr_block.attr,
	&gdlm_attr_withdraw.attr,
540
	&gdlm_attr_jid.attr,
541 542 543 544 545
	&gdlm_attr_first.attr,
	&gdlm_attr_first_done.attr,
	&gdlm_attr_recover.attr,
	&gdlm_attr_recover_done.attr,
	&gdlm_attr_recover_status.attr,
546
	NULL,
David Teigland's avatar
David Teigland committed
547 548 549 550 551 552 553 554
};

/*
 * get and set struct gfs2_tune fields
 */

static ssize_t quota_scale_show(struct gfs2_sbd *sdp, char *buf)
{
555 556 557
	return snprintf(buf, PAGE_SIZE, "%u %u\n",
			sdp->sd_tune.gt_quota_scale_num,
			sdp->sd_tune.gt_quota_scale_den);
David Teigland's avatar
David Teigland committed
558 559 560 561 562 563 564 565 566
}

static ssize_t quota_scale_store(struct gfs2_sbd *sdp, const char *buf,
				 size_t len)
{
	struct gfs2_tune *gt = &sdp->sd_tune;
	unsigned int x, y;

	if (!capable(CAP_SYS_ADMIN))
567
		return -EPERM;
David Teigland's avatar
David Teigland committed
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583

	if (sscanf(buf, "%u %u", &x, &y) != 2 || !y)
		return -EINVAL;

	spin_lock(&gt->gt_spin);
	gt->gt_quota_scale_num = x;
	gt->gt_quota_scale_den = y;
	spin_unlock(&gt->gt_spin);
	return len;
}

static ssize_t tune_set(struct gfs2_sbd *sdp, unsigned int *field,
			int check_zero, const char *buf, size_t len)
{
	struct gfs2_tune *gt = &sdp->sd_tune;
	unsigned int x;
584
	int error;
David Teigland's avatar
David Teigland committed
585 586

	if (!capable(CAP_SYS_ADMIN))
587
		return -EPERM;
David Teigland's avatar
David Teigland committed
588

589 590 591
	error = kstrtouint(buf, 0, &x);
	if (error)
		return error;
David Teigland's avatar
David Teigland committed
592 593 594 595 596 597 598 599 600 601 602

	if (check_zero && !x)
		return -EINVAL;

	spin_lock(&gt->gt_spin);
	*field = x;
	spin_unlock(&gt->gt_spin);
	return len;
}

#define TUNE_ATTR_3(name, show, store)                                        \
603
static struct gfs2_attr tune_attr_##name = __ATTR(name, 0644, show, store)
David Teigland's avatar
David Teigland committed
604 605 606 607

#define TUNE_ATTR_2(name, store)                                              \
static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf)                   \
{                                                                             \
608
	return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name);      \
David Teigland's avatar
David Teigland committed
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
}                                                                             \
TUNE_ATTR_3(name, name##_show, store)

#define TUNE_ATTR(name, check_zero)                                           \
static ssize_t name##_store(struct gfs2_sbd *sdp, const char *buf, size_t len)\
{                                                                             \
	return tune_set(sdp, &sdp->sd_tune.gt_##name, check_zero, buf, len);  \
}                                                                             \
TUNE_ATTR_2(name, name##_store)

TUNE_ATTR(quota_warn_period, 0);
TUNE_ATTR(quota_quantum, 0);
TUNE_ATTR(max_readahead, 0);
TUNE_ATTR(complain_secs, 0);
TUNE_ATTR(statfs_slow, 0);
TUNE_ATTR(new_files_jdata, 0);
TUNE_ATTR(statfs_quantum, 1);
TUNE_ATTR_3(quota_scale, quota_scale_show, quota_scale_store);

static struct attribute *tune_attrs[] = {
	&tune_attr_quota_warn_period.attr,
	&tune_attr_quota_quantum.attr,
	&tune_attr_max_readahead.attr,
	&tune_attr_complain_secs.attr,
	&tune_attr_statfs_slow.attr,
	&tune_attr_statfs_quantum.attr,
	&tune_attr_quota_scale.attr,
	&tune_attr_new_files_jdata.attr,
637
	NULL,
David Teigland's avatar
David Teigland committed
638 639 640 641
};

static struct attribute_group tune_group = {
	.name = "tune",
642
	.attrs = tune_attrs,
David Teigland's avatar
David Teigland committed
643 644
};

645 646 647 648 649
static struct attribute_group lock_module_group = {
	.name = "lock_module",
	.attrs = lock_module_attrs,
};

David Teigland's avatar
David Teigland committed
650 651
int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
{
652
	struct super_block *sb = sdp->sd_vfs;
David Teigland's avatar
David Teigland committed
653
	int error;
654 655 656
	char ro[20];
	char spectator[20];
	char *envp[] = { ro, spectator, NULL };
657
	int sysfs_frees_sdp = 0;
658 659 660

	sprintf(ro, "RDONLY=%d", (sb->s_flags & MS_RDONLY) ? 1 : 0);
	sprintf(spectator, "SPECTATOR=%d", sdp->sd_args.ar_spectator ? 1 : 0);
David Teigland's avatar
David Teigland committed
661

662
	sdp->sd_kobj.kset = gfs2_kset;
663 664
	error = kobject_init_and_add(&sdp->sd_kobj, &gfs2_ktype, NULL,
				     "%s", sdp->sd_table_name);
David Teigland's avatar
David Teigland committed
665
	if (error)
666
		goto fail_reg;
David Teigland's avatar
David Teigland committed
667

668 669
	sysfs_frees_sdp = 1; /* Freeing sdp is now done by sysfs calling
				function gfs2_sbd_release. */
David Teigland's avatar
David Teigland committed
670 671
	error = sysfs_create_group(&sdp->sd_kobj, &tune_group);
	if (error)
672
		goto fail_reg;
David Teigland's avatar
David Teigland committed
673

674 675 676 677
	error = sysfs_create_group(&sdp->sd_kobj, &lock_module_group);
	if (error)
		goto fail_tune;

678 679 680 681 682 683
	error = sysfs_create_link(&sdp->sd_kobj,
				  &disk_to_dev(sb->s_bdev->bd_disk)->kobj,
				  "device");
	if (error)
		goto fail_lock_module;

684
	kobject_uevent_env(&sdp->sd_kobj, KOBJ_ADD, envp);
David Teigland's avatar
David Teigland committed
685 686
	return 0;

687 688
fail_lock_module:
	sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
689 690
fail_tune:
	sysfs_remove_group(&sdp->sd_kobj, &tune_group);
691
fail_reg:
692
	free_percpu(sdp->sd_lkstats);
693
	fs_err(sdp, "error %d adding sysfs files", error);
694 695 696 697 698
	if (sysfs_frees_sdp)
		kobject_put(&sdp->sd_kobj);
	else
		kfree(sdp);
	sb->s_fs_info = NULL;
David Teigland's avatar
David Teigland committed
699 700 701 702 703
	return error;
}

void gfs2_sys_fs_del(struct gfs2_sbd *sdp)
{
704
	sysfs_remove_link(&sdp->sd_kobj, "device");
David Teigland's avatar
David Teigland committed
705
	sysfs_remove_group(&sdp->sd_kobj, &tune_group);
706
	sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
707
	kobject_put(&sdp->sd_kobj);
David Teigland's avatar
David Teigland committed
708 709
}

710 711 712 713
static int gfs2_uevent(struct kset *kset, struct kobject *kobj,
		       struct kobj_uevent_env *env)
{
	struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
714 715
	struct super_block *s = sdp->sd_vfs;
	const u8 *uuid = s->s_uuid;
716

717 718
	add_uevent_var(env, "LOCKTABLE=%s", sdp->sd_table_name);
	add_uevent_var(env, "LOCKPROTO=%s", sdp->sd_proto_name);
719
	if (!test_bit(SDF_NOJOURNALID, &sdp->sd_flags))
720
		add_uevent_var(env, "JOURNALID=%d", sdp->sd_lockstruct.ls_jid);
721 722
	if (gfs2_uuid_valid(uuid))
		add_uevent_var(env, "UUID=%pUB", uuid);
723 724 725
	return 0;
}

726
static const struct kset_uevent_ops gfs2_uevent_ops = {
727 728 729
	.uevent = gfs2_uevent,
};

David Teigland's avatar
David Teigland committed
730 731
int gfs2_sys_init(void)
{
732
	gfs2_kset = kset_create_and_add("gfs2", &gfs2_uevent_ops, fs_kobj);
733 734 735
	if (!gfs2_kset)
		return -ENOMEM;
	return 0;
David Teigland's avatar
David Teigland committed
736 737 738 739
}

void gfs2_sys_uninit(void)
{
740
	kset_unregister(gfs2_kset);
David Teigland's avatar
David Teigland committed
741 742
}