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