xref: /openbmc/linux/fs/gfs2/sys.c (revision 9b68f30b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
4  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
5  */
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <linux/sched.h>
10 #include <linux/cred.h>
11 #include <linux/spinlock.h>
12 #include <linux/completion.h>
13 #include <linux/buffer_head.h>
14 #include <linux/module.h>
15 #include <linux/kobject.h>
16 #include <linux/uaccess.h>
17 #include <linux/gfs2_ondisk.h>
18 #include <linux/blkdev.h>
19 
20 #include "gfs2.h"
21 #include "incore.h"
22 #include "sys.h"
23 #include "super.h"
24 #include "glock.h"
25 #include "quota.h"
26 #include "util.h"
27 #include "glops.h"
28 #include "recovery.h"
29 
30 struct gfs2_attr {
31 	struct attribute attr;
32 	ssize_t (*show)(struct gfs2_sbd *, char *);
33 	ssize_t (*store)(struct gfs2_sbd *, const char *, size_t);
34 };
35 
36 static ssize_t gfs2_attr_show(struct kobject *kobj, struct attribute *attr,
37 			      char *buf)
38 {
39 	struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
40 	struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
41 	return a->show ? a->show(sdp, buf) : 0;
42 }
43 
44 static ssize_t gfs2_attr_store(struct kobject *kobj, struct attribute *attr,
45 			       const char *buf, size_t len)
46 {
47 	struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
48 	struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
49 	return a->store ? a->store(sdp, buf, len) : len;
50 }
51 
52 static const struct sysfs_ops gfs2_attr_ops = {
53 	.show  = gfs2_attr_show,
54 	.store = gfs2_attr_store,
55 };
56 
57 
58 static struct kset *gfs2_kset;
59 
60 static ssize_t id_show(struct gfs2_sbd *sdp, char *buf)
61 {
62 	return snprintf(buf, PAGE_SIZE, "%u:%u\n",
63 			MAJOR(sdp->sd_vfs->s_dev), MINOR(sdp->sd_vfs->s_dev));
64 }
65 
66 static ssize_t status_show(struct gfs2_sbd *sdp, char *buf)
67 {
68 	unsigned long f = sdp->sd_flags;
69 	ssize_t s;
70 
71 	s = snprintf(buf, PAGE_SIZE,
72 		     "Journal Checked:          %d\n"
73 		     "Journal Live:             %d\n"
74 		     "Journal ID:               %d\n"
75 		     "Spectator:                %d\n"
76 		     "Withdrawn:                %d\n"
77 		     "No barriers:              %d\n"
78 		     "No recovery:              %d\n"
79 		     "Demote:                   %d\n"
80 		     "No Journal ID:            %d\n"
81 		     "Mounted RO:               %d\n"
82 		     "RO Recovery:              %d\n"
83 		     "Skip DLM Unlock:          %d\n"
84 		     "Force AIL Flush:          %d\n"
85 		     "FS Frozen:                %d\n"
86 		     "Withdrawing:              %d\n"
87 		     "Withdraw In Prog:         %d\n"
88 		     "Remote Withdraw:          %d\n"
89 		     "Withdraw Recovery:        %d\n"
90 		     "Deactivating:             %d\n"
91 		     "sd_log_error:             %d\n"
92 		     "sd_log_flush_lock:        %d\n"
93 		     "sd_log_num_revoke:        %u\n"
94 		     "sd_log_in_flight:         %d\n"
95 		     "sd_log_blks_needed:       %d\n"
96 		     "sd_log_blks_free:         %d\n"
97 		     "sd_log_flush_head:        %d\n"
98 		     "sd_log_flush_tail:        %d\n"
99 		     "sd_log_blks_reserved:     %d\n"
100 		     "sd_log_revokes_available: %d\n",
101 		     test_bit(SDF_JOURNAL_CHECKED, &f),
102 		     test_bit(SDF_JOURNAL_LIVE, &f),
103 		     (sdp->sd_jdesc ? sdp->sd_jdesc->jd_jid : 0),
104 		     (sdp->sd_args.ar_spectator ? 1 : 0),
105 		     test_bit(SDF_WITHDRAWN, &f),
106 		     test_bit(SDF_NOBARRIERS, &f),
107 		     test_bit(SDF_NORECOVERY, &f),
108 		     test_bit(SDF_DEMOTE, &f),
109 		     test_bit(SDF_NOJOURNALID, &f),
110 		     (sb_rdonly(sdp->sd_vfs) ? 1 : 0),
111 		     test_bit(SDF_RORECOVERY, &f),
112 		     test_bit(SDF_SKIP_DLM_UNLOCK, &f),
113 		     test_bit(SDF_FORCE_AIL_FLUSH, &f),
114 		     test_bit(SDF_FS_FROZEN, &f),
115 		     test_bit(SDF_WITHDRAWING, &f),
116 		     test_bit(SDF_WITHDRAW_IN_PROG, &f),
117 		     test_bit(SDF_REMOTE_WITHDRAW, &f),
118 		     test_bit(SDF_WITHDRAW_RECOVERY, &f),
119 		     test_bit(SDF_DEACTIVATING, &f),
120 		     sdp->sd_log_error,
121 		     rwsem_is_locked(&sdp->sd_log_flush_lock),
122 		     sdp->sd_log_num_revoke,
123 		     atomic_read(&sdp->sd_log_in_flight),
124 		     atomic_read(&sdp->sd_log_blks_needed),
125 		     atomic_read(&sdp->sd_log_blks_free),
126 		     sdp->sd_log_flush_head,
127 		     sdp->sd_log_flush_tail,
128 		     sdp->sd_log_blks_reserved,
129 		     atomic_read(&sdp->sd_log_revokes_available));
130 	return s;
131 }
132 
133 static ssize_t fsname_show(struct gfs2_sbd *sdp, char *buf)
134 {
135 	return snprintf(buf, PAGE_SIZE, "%s\n", sdp->sd_fsname);
136 }
137 
138 static ssize_t uuid_show(struct gfs2_sbd *sdp, char *buf)
139 {
140 	struct super_block *s = sdp->sd_vfs;
141 
142 	buf[0] = '\0';
143 	if (uuid_is_null(&s->s_uuid))
144 		return 0;
145 	return snprintf(buf, PAGE_SIZE, "%pUB\n", &s->s_uuid);
146 }
147 
148 static ssize_t freeze_show(struct gfs2_sbd *sdp, char *buf)
149 {
150 	struct super_block *sb = sdp->sd_vfs;
151 	int frozen = (sb->s_writers.frozen == SB_UNFROZEN) ? 0 : 1;
152 
153 	return snprintf(buf, PAGE_SIZE, "%d\n", frozen);
154 }
155 
156 static ssize_t freeze_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
157 {
158 	int error, n;
159 
160 	error = kstrtoint(buf, 0, &n);
161 	if (error)
162 		return error;
163 
164 	if (!capable(CAP_SYS_ADMIN))
165 		return -EPERM;
166 
167 	switch (n) {
168 	case 0:
169 		error = thaw_super(sdp->sd_vfs);
170 		break;
171 	case 1:
172 		error = freeze_super(sdp->sd_vfs);
173 		break;
174 	default:
175 		return -EINVAL;
176 	}
177 
178 	if (error) {
179 		fs_warn(sdp, "freeze %d error %d\n", n, error);
180 		return error;
181 	}
182 
183 	return len;
184 }
185 
186 static ssize_t withdraw_show(struct gfs2_sbd *sdp, char *buf)
187 {
188 	unsigned int b = gfs2_withdrawn(sdp);
189 	return snprintf(buf, PAGE_SIZE, "%u\n", b);
190 }
191 
192 static ssize_t withdraw_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
193 {
194 	int error, val;
195 
196 	if (!capable(CAP_SYS_ADMIN))
197 		return -EPERM;
198 
199 	error = kstrtoint(buf, 0, &val);
200 	if (error)
201 		return error;
202 
203 	if (val != 1)
204 		return -EINVAL;
205 
206 	gfs2_lm(sdp, "withdrawing from cluster at user's request\n");
207 	gfs2_withdraw(sdp);
208 
209 	return len;
210 }
211 
212 static ssize_t statfs_sync_store(struct gfs2_sbd *sdp, const char *buf,
213 				 size_t len)
214 {
215 	int error, val;
216 
217 	if (!capable(CAP_SYS_ADMIN))
218 		return -EPERM;
219 
220 	error = kstrtoint(buf, 0, &val);
221 	if (error)
222 		return error;
223 
224 	if (val != 1)
225 		return -EINVAL;
226 
227 	gfs2_statfs_sync(sdp->sd_vfs, 0);
228 	return len;
229 }
230 
231 static ssize_t quota_sync_store(struct gfs2_sbd *sdp, const char *buf,
232 				size_t len)
233 {
234 	int error, val;
235 
236 	if (!capable(CAP_SYS_ADMIN))
237 		return -EPERM;
238 
239 	error = kstrtoint(buf, 0, &val);
240 	if (error)
241 		return error;
242 
243 	if (val != 1)
244 		return -EINVAL;
245 
246 	gfs2_quota_sync(sdp->sd_vfs, 0);
247 	return len;
248 }
249 
250 static ssize_t quota_refresh_user_store(struct gfs2_sbd *sdp, const char *buf,
251 					size_t len)
252 {
253 	struct kqid qid;
254 	int error;
255 	u32 id;
256 
257 	if (!capable(CAP_SYS_ADMIN))
258 		return -EPERM;
259 
260 	error = kstrtou32(buf, 0, &id);
261 	if (error)
262 		return error;
263 
264 	qid = make_kqid(current_user_ns(), USRQUOTA, id);
265 	if (!qid_valid(qid))
266 		return -EINVAL;
267 
268 	error = gfs2_quota_refresh(sdp, qid);
269 	return error ? error : len;
270 }
271 
272 static ssize_t quota_refresh_group_store(struct gfs2_sbd *sdp, const char *buf,
273 					 size_t len)
274 {
275 	struct kqid qid;
276 	int error;
277 	u32 id;
278 
279 	if (!capable(CAP_SYS_ADMIN))
280 		return -EPERM;
281 
282 	error = kstrtou32(buf, 0, &id);
283 	if (error)
284 		return error;
285 
286 	qid = make_kqid(current_user_ns(), GRPQUOTA, id);
287 	if (!qid_valid(qid))
288 		return -EINVAL;
289 
290 	error = gfs2_quota_refresh(sdp, qid);
291 	return error ? error : len;
292 }
293 
294 static ssize_t demote_rq_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
295 {
296 	struct gfs2_glock *gl;
297 	const struct gfs2_glock_operations *glops;
298 	unsigned int glmode;
299 	unsigned int gltype;
300 	unsigned long long glnum;
301 	char mode[16];
302 	int rv;
303 
304 	if (!capable(CAP_SYS_ADMIN))
305 		return -EPERM;
306 
307 	rv = sscanf(buf, "%u:%llu %15s", &gltype, &glnum,
308 		    mode);
309 	if (rv != 3)
310 		return -EINVAL;
311 
312 	if (strcmp(mode, "EX") == 0)
313 		glmode = LM_ST_UNLOCKED;
314 	else if ((strcmp(mode, "CW") == 0) || (strcmp(mode, "DF") == 0))
315 		glmode = LM_ST_DEFERRED;
316 	else if ((strcmp(mode, "PR") == 0) || (strcmp(mode, "SH") == 0))
317 		glmode = LM_ST_SHARED;
318 	else
319 		return -EINVAL;
320 
321 	if (gltype > LM_TYPE_JOURNAL)
322 		return -EINVAL;
323 	if (gltype == LM_TYPE_NONDISK && glnum == GFS2_FREEZE_LOCK)
324 		glops = &gfs2_freeze_glops;
325 	else
326 		glops = gfs2_glops_list[gltype];
327 	if (glops == NULL)
328 		return -EINVAL;
329 	if (!test_and_set_bit(SDF_DEMOTE, &sdp->sd_flags))
330 		fs_info(sdp, "demote interface used\n");
331 	rv = gfs2_glock_get(sdp, glnum, glops, 0, &gl);
332 	if (rv)
333 		return rv;
334 	gfs2_glock_cb(gl, glmode);
335 	gfs2_glock_put(gl);
336 	return len;
337 }
338 
339 
340 #define GFS2_ATTR(name, mode, show, store) \
341 static struct gfs2_attr gfs2_attr_##name = __ATTR(name, mode, show, store)
342 
343 GFS2_ATTR(id,                  0444, id_show,       NULL);
344 GFS2_ATTR(fsname,              0444, fsname_show,   NULL);
345 GFS2_ATTR(uuid,                0444, uuid_show,     NULL);
346 GFS2_ATTR(freeze,              0644, freeze_show,   freeze_store);
347 GFS2_ATTR(withdraw,            0644, withdraw_show, withdraw_store);
348 GFS2_ATTR(statfs_sync,         0200, NULL,          statfs_sync_store);
349 GFS2_ATTR(quota_sync,          0200, NULL,          quota_sync_store);
350 GFS2_ATTR(quota_refresh_user,  0200, NULL,          quota_refresh_user_store);
351 GFS2_ATTR(quota_refresh_group, 0200, NULL,          quota_refresh_group_store);
352 GFS2_ATTR(demote_rq,           0200, NULL,	    demote_rq_store);
353 GFS2_ATTR(status,              0400, status_show,   NULL);
354 
355 static struct attribute *gfs2_attrs[] = {
356 	&gfs2_attr_id.attr,
357 	&gfs2_attr_fsname.attr,
358 	&gfs2_attr_uuid.attr,
359 	&gfs2_attr_freeze.attr,
360 	&gfs2_attr_withdraw.attr,
361 	&gfs2_attr_statfs_sync.attr,
362 	&gfs2_attr_quota_sync.attr,
363 	&gfs2_attr_quota_refresh_user.attr,
364 	&gfs2_attr_quota_refresh_group.attr,
365 	&gfs2_attr_demote_rq.attr,
366 	&gfs2_attr_status.attr,
367 	NULL,
368 };
369 ATTRIBUTE_GROUPS(gfs2);
370 
371 static 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 
378 static struct kobj_type gfs2_ktype = {
379 	.release = gfs2_sbd_release,
380 	.default_groups = gfs2_groups,
381 	.sysfs_ops     = &gfs2_attr_ops,
382 };
383 
384 
385 /*
386  * lock_module. Originally from lock_dlm
387  */
388 
389 static 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 
395 static 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 
407 static 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 
428 static 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 
435 static 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 
451 static 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 
457 static 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;
479 out:
480         spin_unlock(&sdp->sd_jindex_spin);
481         return rv ? rv : len;
482 }
483 
484 static 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 
490 int 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 	}
518 out:
519 	spin_unlock(&sdp->sd_jindex_spin);
520 	return rv;
521 }
522 
523 static 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);
538 out:
539 	return rv ? rv : len;
540 }
541 
542 static 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 
548 static 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 
554 static ssize_t jid_show(struct gfs2_sbd *sdp, char *buf)
555 {
556 	return sprintf(buf, "%d\n", sdp->sd_lockstruct.ls_jid);
557 }
558 
559 static 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);
584 out:
585 	spin_unlock(&sdp->sd_jindex_spin);
586 	return rv ? rv : len;
587 }
588 
589 #define GDLM_ATTR(_name,_mode,_show,_store) \
590 static struct gfs2_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
591 
592 GDLM_ATTR(proto_name,		0444, proto_name_show,		NULL);
593 GDLM_ATTR(block,		0644, block_show,		block_store);
594 GDLM_ATTR(withdraw,		0644, wdack_show,		wdack_store);
595 GDLM_ATTR(jid,			0644, jid_show,			jid_store);
596 GDLM_ATTR(first,		0644, lkfirst_show,		lkfirst_store);
597 GDLM_ATTR(first_done,		0444, first_done_show,		NULL);
598 GDLM_ATTR(recover,		0600, NULL,			recover_store);
599 GDLM_ATTR(recover_done,		0444, recover_done_show,	NULL);
600 GDLM_ATTR(recover_status,	0444, recover_status_show,	NULL);
601 
602 static 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 
619 static 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 
626 static 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 
645 static 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)                                        \
669 static struct gfs2_attr tune_attr_##name = __ATTR(name, 0644, show, store)
670 
671 #define TUNE_ATTR_2(name, store)                                              \
672 static 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 }                                                                             \
676 TUNE_ATTR_3(name, name##_show, store)
677 
678 #define TUNE_ATTR(name, check_zero)                                           \
679 static 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 }                                                                             \
683 TUNE_ATTR_2(name, name##_store)
684 
685 TUNE_ATTR(quota_warn_period, 0);
686 TUNE_ATTR(quota_quantum, 0);
687 TUNE_ATTR(max_readahead, 0);
688 TUNE_ATTR(complain_secs, 0);
689 TUNE_ATTR(statfs_slow, 0);
690 TUNE_ATTR(new_files_jdata, 0);
691 TUNE_ATTR(statfs_quantum, 1);
692 TUNE_ATTR_3(quota_scale, quota_scale_show, quota_scale_store);
693 
694 static 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 
706 static const struct attribute_group tune_group = {
707 	.name = "tune",
708 	.attrs = tune_attrs,
709 };
710 
711 static const struct attribute_group lock_module_group = {
712 	.name = "lock_module",
713 	.attrs = lock_module_attrs,
714 };
715 
716 int 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 
751 fail_lock_module:
752 	sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
753 fail_tune:
754 	sysfs_remove_group(&sdp->sd_kobj, &tune_group);
755 fail_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 
763 void 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 
772 static int gfs2_uevent(const struct kobject *kobj, struct kobj_uevent_env *env)
773 {
774 	const struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
775 	const struct super_block *s = sdp->sd_vfs;
776 
777 	add_uevent_var(env, "LOCKTABLE=%s", sdp->sd_table_name);
778 	add_uevent_var(env, "LOCKPROTO=%s", sdp->sd_proto_name);
779 	if (!test_bit(SDF_NOJOURNALID, &sdp->sd_flags))
780 		add_uevent_var(env, "JOURNALID=%d", sdp->sd_lockstruct.ls_jid);
781 	if (!uuid_is_null(&s->s_uuid))
782 		add_uevent_var(env, "UUID=%pUB", &s->s_uuid);
783 	return 0;
784 }
785 
786 static const struct kset_uevent_ops gfs2_uevent_ops = {
787 	.uevent = gfs2_uevent,
788 };
789 
790 int gfs2_sys_init(void)
791 {
792 	gfs2_kset = kset_create_and_add("gfs2", &gfs2_uevent_ops, fs_kobj);
793 	if (!gfs2_kset)
794 		return -ENOMEM;
795 	return 0;
796 }
797 
798 void gfs2_sys_uninit(void)
799 {
800 	kset_unregister(gfs2_kset);
801 }
802 
803