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