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