blob: c3e72dba7418ae4af3781eb32d3b0adb27c7868d [file] [log] [blame]
Thomas Gleixner7336d0e2019-05-31 01:09:56 -07001// SPDX-License-Identifier: GPL-2.0-only
David Teiglandb3b94fa2006-01-16 16:50:04 +00002/*
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04004 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00005 */
6
Joe Perchesd77d1b52014-03-06 12:10:45 -08007#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
David Teiglandb3b94fa2006-01-16 16:50:04 +00009#include <linux/sched.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010010#include <linux/cred.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000011#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>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080016#include <linux/uaccess.h>
Steven Whitehousef057f6c2009-01-12 10:43:39 +000017#include <linux/gfs2_ondisk.h>
Steven Whitehouse31e54b02009-08-13 12:18:08 +010018#include <linux/genhd.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000019
20#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050021#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include "sys.h"
23#include "super.h"
24#include "glock.h"
25#include "quota.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050026#include "util.h"
Steven Whitehouse64d576b2009-02-12 13:31:58 +000027#include "glops.h"
Tejun Heo6ecd7c22010-07-20 22:09:02 +020028#include "recovery.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000029
Steven Whitehouse48c2b612009-05-13 14:49:48 +010030struct 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
Emese Revfy52cf25d2010-01-19 02:58:23 +010052static const struct sysfs_ops gfs2_attr_ops = {
Steven Whitehouse48c2b612009-05-13 14:49:48 +010053 .show = gfs2_attr_show,
54 .store = gfs2_attr_store,
55};
56
57
58static struct kset *gfs2_kset;
59
David Teiglandb3b94fa2006-01-16 16:50:04 +000060static ssize_t id_show(struct gfs2_sbd *sdp, char *buf)
61{
Bob Petersonc7227e462007-11-02 09:37:15 -050062 return snprintf(buf, PAGE_SIZE, "%u:%u\n",
63 MAJOR(sdp->sd_vfs->s_dev), MINOR(sdp->sd_vfs->s_dev));
David Teiglandb3b94fa2006-01-16 16:50:04 +000064}
65
66static ssize_t fsname_show(struct gfs2_sbd *sdp, char *buf)
67{
David Teigland3204a6c2006-09-06 16:57:06 -050068 return snprintf(buf, PAGE_SIZE, "%s\n", sdp->sd_fsname);
David Teiglandb3b94fa2006-01-16 16:50:04 +000069}
70
Steven Whitehouse02e3cc72009-02-10 13:48:30 +000071static ssize_t uuid_show(struct gfs2_sbd *sdp, char *buf)
72{
Steven Whitehouse32e471e2011-05-10 15:01:59 +010073 struct super_block *s = sdp->sd_vfs;
Christoph Hellwig85787092017-05-10 15:06:33 +020074
Steven Whitehouse02e3cc72009-02-10 13:48:30 +000075 buf[0] = '\0';
Christoph Hellwig85787092017-05-10 15:06:33 +020076 if (uuid_is_null(&s->s_uuid))
Steven Whitehouse02e3cc72009-02-10 13:48:30 +000077 return 0;
Christoph Hellwig85787092017-05-10 15:06:33 +020078 return snprintf(buf, PAGE_SIZE, "%pUB\n", &s->s_uuid);
Steven Whitehouse02e3cc72009-02-10 13:48:30 +000079}
80
David Teiglandb3b94fa2006-01-16 16:50:04 +000081static ssize_t freeze_show(struct gfs2_sbd *sdp, char *buf)
82{
Steven Whitehoused564053f2013-01-11 10:49:34 +000083 struct super_block *sb = sdp->sd_vfs;
84 int frozen = (sb->s_writers.frozen == SB_UNFROZEN) ? 0 : 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +000085
alex chen3566c962015-01-12 19:01:03 +080086 return snprintf(buf, PAGE_SIZE, "%d\n", frozen);
David Teiglandb3b94fa2006-01-16 16:50:04 +000087}
88
89static ssize_t freeze_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
90{
Fabian Fredericke50ead42015-05-05 13:23:22 -050091 int error, n;
92
93 error = kstrtoint(buf, 0, &n);
94 if (error)
95 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +000096
97 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +110098 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +000099
100 switch (n) {
101 case 0:
Steven Whitehoused564053f2013-01-11 10:49:34 +0000102 error = thaw_super(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000103 break;
104 case 1:
Steven Whitehoused564053f2013-01-11 10:49:34 +0000105 error = freeze_super(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000106 break;
107 default:
Steven Whitehoused564053f2013-01-11 10:49:34 +0000108 return -EINVAL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109 }
110
Steven Whitehoused564053f2013-01-11 10:49:34 +0000111 if (error) {
Andreas Gruenbacheraf388162018-01-30 10:32:30 -0700112 fs_warn(sdp, "freeze %d error %d\n", n, error);
Steven Whitehoused564053f2013-01-11 10:49:34 +0000113 return error;
114 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000115
Steven Whitehoused564053f2013-01-11 10:49:34 +0000116 return len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000117}
118
119static ssize_t withdraw_show(struct gfs2_sbd *sdp, char *buf)
120{
Bob Petersoneb43e662019-11-14 09:52:15 -0500121 unsigned int b = gfs2_withdrawn(sdp);
David Teigland3204a6c2006-09-06 16:57:06 -0500122 return snprintf(buf, PAGE_SIZE, "%u\n", b);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000123}
124
125static ssize_t withdraw_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
126{
Fabian Fredericke50ead42015-05-05 13:23:22 -0500127 int error, val;
128
David Teiglandb3b94fa2006-01-16 16:50:04 +0000129 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100130 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000131
Fabian Fredericke50ead42015-05-05 13:23:22 -0500132 error = kstrtoint(buf, 0, &val);
133 if (error)
134 return error;
135
136 if (val != 1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000137 return -EINVAL;
138
Andreas Gruenbacherbadb55e2020-01-23 18:41:00 +0100139 gfs2_lm(sdp, "withdrawing from cluster at user's request\n");
140 gfs2_withdraw(sdp);
Joe Perchescb94eb02014-03-06 12:17:21 -0800141
David Teiglandb3b94fa2006-01-16 16:50:04 +0000142 return len;
143}
144
145static ssize_t statfs_sync_store(struct gfs2_sbd *sdp, const char *buf,
146 size_t len)
147{
Fabian Fredericke50ead42015-05-05 13:23:22 -0500148 int error, val;
149
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100151 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152
Fabian Fredericke50ead42015-05-05 13:23:22 -0500153 error = kstrtoint(buf, 0, &val);
154 if (error)
155 return error;
156
157 if (val != 1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000158 return -EINVAL;
159
Steven Whitehouse8c42d632009-09-11 14:36:44 +0100160 gfs2_statfs_sync(sdp->sd_vfs, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161 return len;
162}
163
David Teiglandb3b94fa2006-01-16 16:50:04 +0000164static ssize_t quota_sync_store(struct gfs2_sbd *sdp, const char *buf,
165 size_t len)
166{
Fabian Fredericke50ead42015-05-05 13:23:22 -0500167 int error, val;
168
David Teiglandb3b94fa2006-01-16 16:50:04 +0000169 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100170 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000171
Fabian Fredericke50ead42015-05-05 13:23:22 -0500172 error = kstrtoint(buf, 0, &val);
173 if (error)
174 return error;
175
176 if (val != 1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000177 return -EINVAL;
178
Jan Karaceed1722012-07-03 16:45:28 +0200179 gfs2_quota_sync(sdp->sd_vfs, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000180 return len;
181}
182
183static ssize_t quota_refresh_user_store(struct gfs2_sbd *sdp, const char *buf,
184 size_t len)
185{
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800186 struct kqid qid;
Steven Whitehouseea762332009-09-15 16:20:30 +0100187 int error;
Steven Whitehousecd915492006-09-04 12:49:07 -0400188 u32 id;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000189
190 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100191 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000192
Fabian Fredericke50ead42015-05-05 13:23:22 -0500193 error = kstrtou32(buf, 0, &id);
194 if (error)
195 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000196
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800197 qid = make_kqid(current_user_ns(), USRQUOTA, id);
198 if (!qid_valid(qid))
199 return -EINVAL;
200
201 error = gfs2_quota_refresh(sdp, qid);
Steven Whitehouseea762332009-09-15 16:20:30 +0100202 return error ? error : len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000203}
204
205static ssize_t quota_refresh_group_store(struct gfs2_sbd *sdp, const char *buf,
206 size_t len)
207{
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800208 struct kqid qid;
Steven Whitehouseea762332009-09-15 16:20:30 +0100209 int error;
Steven Whitehousecd915492006-09-04 12:49:07 -0400210 u32 id;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000211
212 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100213 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000214
Fabian Fredericke50ead42015-05-05 13:23:22 -0500215 error = kstrtou32(buf, 0, &id);
216 if (error)
217 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000218
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800219 qid = make_kqid(current_user_ns(), GRPQUOTA, id);
220 if (!qid_valid(qid))
221 return -EINVAL;
222
223 error = gfs2_quota_refresh(sdp, qid);
Steven Whitehouseea762332009-09-15 16:20:30 +0100224 return error ? error : len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000225}
226
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000227static 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))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100238 return -EPERM;
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000239
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;
Benjamin Marzinski24972552014-05-01 22:26:55 -0500256 if (gltype == LM_TYPE_NONDISK && glnum == GFS2_FREEZE_LOCK)
257 glops = &gfs2_freeze_glops;
Steven Whitehouse13466982010-10-06 09:58:44 +0100258 else
259 glops = gfs2_glops_list[gltype];
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000260 if (glops == NULL)
261 return -EINVAL;
Steven Whitehouse6a99be52010-05-14 14:05:51 +0100262 if (!test_and_set_bit(SDF_DEMOTE, &sdp->sd_flags))
Steven Whitehouse913a71d2010-05-06 11:03:29 +0100263 fs_info(sdp, "demote interface used\n");
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000264 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
David Teiglandb3b94fa2006-01-16 16:50:04 +0000272
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);
Steven Whitehouse02e3cc72009-02-10 13:48:30 +0000278GFS2_ATTR(uuid, 0444, uuid_show, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000279GFS2_ATTR(freeze, 0644, freeze_show, freeze_store);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000280GFS2_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);
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000285GFS2_ATTR(demote_rq, 0200, NULL, demote_rq_store);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000286
287static struct attribute *gfs2_attrs[] = {
288 &gfs2_attr_id.attr,
289 &gfs2_attr_fsname.attr,
Steven Whitehouse02e3cc72009-02-10 13:48:30 +0000290 &gfs2_attr_uuid.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000291 &gfs2_attr_freeze.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000292 &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,
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000297 &gfs2_attr_demote_rq.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000298 NULL,
299};
Kimberly Brownef254d12019-06-07 14:23:00 -0400300ATTRIBUTE_GROUPS(gfs2);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000301
Bob Peterson0d515212012-06-13 10:27:41 -0400302static void gfs2_sbd_release(struct kobject *kobj)
303{
304 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
305
Jamie Ilesc2a04b02020-10-12 14:13:09 +0100306 complete(&sdp->sd_kobj_unregister);
Bob Peterson0d515212012-06-13 10:27:41 -0400307}
308
David Teiglandb3b94fa2006-01-16 16:50:04 +0000309static struct kobj_type gfs2_ktype = {
Bob Peterson0d515212012-06-13 10:27:41 -0400310 .release = gfs2_sbd_release,
Kimberly Brownef254d12019-06-07 14:23:00 -0400311 .default_groups = gfs2_groups,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000312 .sysfs_ops = &gfs2_attr_ops,
313};
314
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000315
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
David Teiglande0c2a9a2012-01-09 17:18:05 -0500332 if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000333 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;
Fabian Fredericke50ead42015-05-05 13:23:22 -0500341 int ret, val;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000342
Fabian Fredericke50ead42015-05-05 13:23:22 -0500343 ret = kstrtoint(buf, 0, &val);
344 if (ret)
345 return ret;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000346
347 if (val == 1)
David Teiglande0c2a9a2012-01-09 17:18:05 -0500348 set_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000349 else if (val == 0) {
David Teiglande0c2a9a2012-01-09 17:18:05 -0500350 clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100351 smp_mb__after_atomic();
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000352 gfs2_glock_thaw(sdp);
353 } else {
Fabian Fredericke50ead42015-05-05 13:23:22 -0500354 return -EINVAL;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000355 }
Fabian Fredericke50ead42015-05-05 13:23:22 -0500356 return len;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000357}
358
Steven Whitehousefd95e812013-02-13 12:21:40 +0000359static 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{
Fabian Fredericke50ead42015-05-05 13:23:22 -0500368 int ret, val;
Steven Whitehousefd95e812013-02-13 12:21:40 +0000369
Fabian Fredericke50ead42015-05-05 13:23:22 -0500370 ret = kstrtoint(buf, 0, &val);
371 if (ret)
372 return ret;
Steven Whitehousefd95e812013-02-13 12:21:40 +0000373
374 if ((val == 1) &&
375 !strcmp(sdp->sd_lockstruct.ls_ops->lm_proto_name, "lock_dlm"))
376 complete(&sdp->sd_wdack);
377 else
Fabian Fredericke50ead42015-05-05 13:23:22 -0500378 return -EINVAL;
379 return len;
Steven Whitehousefd95e812013-02-13 12:21:40 +0000380}
381
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000382static 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
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100388static 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;
Steven Whitehouse3942ae532011-07-11 08:53:30 +0100396 rv = wait_for_completion_killable(&sdp->sd_locking_init);
397 if (rv)
398 return rv;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100399 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;
David Teiglande0c2a9a2012-01-09 17:18:05 -0500408 sdp->sd_lockstruct.ls_first = first;
409 rv = 0;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100410out:
411 spin_unlock(&sdp->sd_jindex_spin);
412 return rv ? rv : len;
413}
414
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000415static ssize_t first_done_show(struct gfs2_sbd *sdp, char *buf)
416{
417 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
David Teiglande0c2a9a2012-01-09 17:18:05 -0500418 return sprintf(buf, "%d\n", !!test_bit(DFL_FIRST_MOUNT_DONE, &ls->ls_recover_flags));
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000419}
420
David Teiglande0c2a9a2012-01-09 17:18:05 -0500421int gfs2_recover_set(struct gfs2_sbd *sdp, unsigned jid)
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000422{
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000423 struct gfs2_jdesc *jd;
Steven Whitehousefe64d512009-05-19 10:01:18 +0100424 int rv;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000425
Bob Peterson0e48e0552014-06-02 09:40:25 -0400426 /* Wait for our primary journal to be initialized */
427 wait_for_completion(&sdp->sd_journal_ready);
428
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000429 spin_lock(&sdp->sd_jindex_spin);
Steven Whitehousefe64d512009-05-19 10:01:18 +0100430 rv = -EBUSY;
Bob Peterson4a772772018-07-05 14:40:46 -0500431 /**
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 */
Bob Peterson601ef0d2020-01-28 20:23:45 +0100438 if (!sdp->sd_jdesc)
439 goto out;
Bob Peterson4a772772018-07-05 14:40:46 -0500440 if (sdp->sd_jdesc->jd_jid == jid && !sdp->sd_args.ar_spectator)
Steven Whitehousefe64d512009-05-19 10:01:18 +0100441 goto out;
442 rv = -ENOENT;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000443 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
Bob Peterson4a772772018-07-05 14:40:46 -0500444 if (jd->jd_jid != jid && !sdp->sd_args.ar_spectator)
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000445 continue;
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200446 rv = gfs2_recover_journal(jd, false);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000447 break;
448 }
Steven Whitehousefe64d512009-05-19 10:01:18 +0100449out:
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000450 spin_unlock(&sdp->sd_jindex_spin);
David Teiglande0c2a9a2012-01-09 17:18:05 -0500451 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
David Teigland1a058f52012-05-01 15:50:48 -0500463 if (test_bit(SDF_NORECOVERY, &sdp->sd_flags)) {
464 rv = -ESHUTDOWN;
465 goto out;
466 }
David Teiglande0c2a9a2012-01-09 17:18:05 -0500467
David Teigland1a058f52012-05-01 15:50:48 -0500468 rv = gfs2_recover_set(sdp, jid);
469out:
Steven Whitehousefe64d512009-05-19 10:01:18 +0100470 return rv ? rv : len;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000471}
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
Steven Whitehousee1b28aa2009-05-26 15:41:27 +0100485static ssize_t jid_show(struct gfs2_sbd *sdp, char *buf)
486{
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100487 return sprintf(buf, "%d\n", sdp->sd_lockstruct.ls_jid);
Steven Whitehousee1b28aa2009-05-26 15:41:27 +0100488}
489
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100490static ssize_t jid_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
491{
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100492 int jid;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100493 int rv;
494
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100495 rv = sscanf(buf, "%d", &jid);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100496 if (rv != 1)
497 return -EINVAL;
Steven Whitehouse3942ae532011-07-11 08:53:30 +0100498 rv = wait_for_completion_killable(&sdp->sd_locking_init);
499 if (rv)
500 return rv;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100501 spin_lock(&sdp->sd_jindex_spin);
502 rv = -EINVAL;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100503 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
504 goto out;
505 rv = -EBUSY;
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100506 if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100507 goto out;
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100508 rv = 0;
509 if (sdp->sd_args.ar_spectator && jid > 0)
510 rv = jid = -EINVAL;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100511 sdp->sd_lockstruct.ls_jid = jid;
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100512 clear_bit(SDF_NOJOURNALID, &sdp->sd_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100513 smp_mb__after_atomic();
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100514 wake_up_bit(&sdp->sd_flags, SDF_NOJOURNALID);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100515out:
516 spin_unlock(&sdp->sd_jindex_spin);
517 return rv ? rv : len;
518}
519
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000520#define GDLM_ATTR(_name,_mode,_show,_store) \
Steven Whitehouse48c2b612009-05-13 14:49:48 +0100521static struct gfs2_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000522
Steven Whitehoused7e623d2009-08-11 11:20:11 +0100523GDLM_ATTR(proto_name, 0444, proto_name_show, NULL);
524GDLM_ATTR(block, 0644, block_show, block_store);
Steven Whitehousefd95e812013-02-13 12:21:40 +0000525GDLM_ATTR(withdraw, 0644, wdack_show, wdack_store);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100526GDLM_ATTR(jid, 0644, jid_show, jid_store);
527GDLM_ATTR(first, 0644, lkfirst_show, lkfirst_store);
Steven Whitehoused7e623d2009-08-11 11:20:11 +0100528GDLM_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);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000532
533static struct attribute *lock_module_attrs[] = {
534 &gdlm_attr_proto_name.attr,
535 &gdlm_attr_block.attr,
536 &gdlm_attr_withdraw.attr,
Steven Whitehousee1b28aa2009-05-26 15:41:27 +0100537 &gdlm_attr_jid.attr,
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000538 &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,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400543 NULL,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000544};
545
David Teiglandb3b94fa2006-01-16 16:50:04 +0000546/*
David Teiglandb3b94fa2006-01-16 16:50:04 +0000547 * get and set struct gfs2_tune fields
548 */
549
550static ssize_t quota_scale_show(struct gfs2_sbd *sdp, char *buf)
551{
David Teigland3204a6c2006-09-06 16:57:06 -0500552 return snprintf(buf, PAGE_SIZE, "%u %u\n",
553 sdp->sd_tune.gt_quota_scale_num,
554 sdp->sd_tune.gt_quota_scale_den);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000555}
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))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100564 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565
566 if (sscanf(buf, "%u %u", &x, &y) != 2 || !y)
567 return -EINVAL;
568
569 spin_lock(&gt->gt_spin);
570 gt->gt_quota_scale_num = x;
571 gt->gt_quota_scale_den = y;
572 spin_unlock(&gt->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;
Fabian Fredericke50ead42015-05-05 13:23:22 -0500581 int error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000582
583 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100584 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585
Fabian Fredericke50ead42015-05-05 13:23:22 -0500586 error = kstrtouint(buf, 0, &x);
587 if (error)
588 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589
590 if (check_zero && !x)
591 return -EINVAL;
592
593 spin_lock(&gt->gt_spin);
594 *field = x;
595 spin_unlock(&gt->gt_spin);
596 return len;
597}
598
David Teiglandb3b94fa2006-01-16 16:50:04 +0000599#define TUNE_ATTR_3(name, show, store) \
Steven Whitehouse48c2b612009-05-13 14:49:48 +0100600static struct gfs2_attr tune_attr_##name = __ATTR(name, 0644, show, store)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000601
602#define TUNE_ATTR_2(name, store) \
603static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf) \
604{ \
David Teigland3204a6c2006-09-06 16:57:06 -0500605 return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name); \
David Teiglandb3b94fa2006-01-16 16:50:04 +0000606} \
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
David Teiglandb3b94fa2006-01-16 16:50:04 +0000616TUNE_ATTR(quota_warn_period, 0);
617TUNE_ATTR(quota_quantum, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000618TUNE_ATTR(max_readahead, 0);
619TUNE_ATTR(complain_secs, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000620TUNE_ATTR(statfs_slow, 0);
621TUNE_ATTR(new_files_jdata, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000622TUNE_ATTR(statfs_quantum, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000623TUNE_ATTR_3(quota_scale, quota_scale_show, quota_scale_store);
624
625static struct attribute *tune_attrs[] = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000626 &tune_attr_quota_warn_period.attr,
627 &tune_attr_quota_quantum.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000628 &tune_attr_max_readahead.attr,
629 &tune_attr_complain_secs.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000630 &tune_attr_statfs_slow.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000631 &tune_attr_statfs_quantum.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000632 &tune_attr_quota_scale.attr,
633 &tune_attr_new_files_jdata.attr,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400634 NULL,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000635};
636
Arvind Yadav29695252017-06-30 08:33:54 -0500637static const struct attribute_group tune_group = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000638 .name = "tune",
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400639 .attrs = tune_attrs,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000640};
641
Arvind Yadav29695252017-06-30 08:33:54 -0500642static const struct attribute_group lock_module_group = {
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000643 .name = "lock_module",
644 .attrs = lock_module_attrs,
645};
646
David Teiglandb3b94fa2006-01-16 16:50:04 +0000647int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
648{
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100649 struct super_block *sb = sdp->sd_vfs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650 int error;
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100651 char ro[20];
652 char spectator[20];
653 char *envp[] = { ro, spectator, NULL };
654
David Howellsbc98a422017-07-17 08:45:34 +0100655 sprintf(ro, "RDONLY=%d", sb_rdonly(sb));
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100656 sprintf(spectator, "SPECTATOR=%d", sdp->sd_args.ar_spectator ? 1 : 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000657
Jamie Ilesc2a04b02020-10-12 14:13:09 +0100658 init_completion(&sdp->sd_kobj_unregister);
Greg Kroah-Hartman9bec1012007-10-29 20:13:17 +0100659 sdp->sd_kobj.kset = gfs2_kset;
Greg Kroah-Hartman901195e2007-12-17 15:54:39 -0400660 error = kobject_init_and_add(&sdp->sd_kobj, &gfs2_ktype, NULL,
661 "%s", sdp->sd_table_name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000662 if (error)
Bob Peterson0d515212012-06-13 10:27:41 -0400663 goto fail_reg;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000664
David Teiglandb3b94fa2006-01-16 16:50:04 +0000665 error = sysfs_create_group(&sdp->sd_kobj, &tune_group);
666 if (error)
Steven Whitehousef6eb5342009-05-26 15:50:25 +0100667 goto fail_reg;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000668
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000669 error = sysfs_create_group(&sdp->sd_kobj, &lock_module_group);
670 if (error)
671 goto fail_tune;
672
Steven Whitehouse31e54b02009-08-13 12:18:08 +0100673 error = sysfs_create_link(&sdp->sd_kobj,
674 &disk_to_dev(sb->s_bdev->bd_disk)->kobj,
675 "device");
676 if (error)
677 goto fail_lock_module;
678
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100679 kobject_uevent_env(&sdp->sd_kobj, KOBJ_ADD, envp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000680 return 0;
681
Steven Whitehouse31e54b02009-08-13 12:18:08 +0100682fail_lock_module:
683 sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000684fail_tune:
685 sysfs_remove_group(&sdp->sd_kobj, &tune_group);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400686fail_reg:
Andreas Gruenbacheraf388162018-01-30 10:32:30 -0700687 fs_err(sdp, "error %d adding sysfs files\n", error);
Tobin C. Hardingfbcde192019-05-13 21:59:04 +0200688 kobject_put(&sdp->sd_kobj);
Jamie Ilesc2a04b02020-10-12 14:13:09 +0100689 wait_for_completion(&sdp->sd_kobj_unregister);
Bob Peterson0d515212012-06-13 10:27:41 -0400690 sb->s_fs_info = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691 return error;
692}
693
694void gfs2_sys_fs_del(struct gfs2_sbd *sdp)
695{
Steven Whitehouse31e54b02009-08-13 12:18:08 +0100696 sysfs_remove_link(&sdp->sd_kobj, "device");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697 sysfs_remove_group(&sdp->sd_kobj, &tune_group);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000698 sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800699 kobject_put(&sdp->sd_kobj);
Jamie Ilesc2a04b02020-10-12 14:13:09 +0100700 wait_for_completion(&sdp->sd_kobj_unregister);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000701}
702
Steven Whitehousefdd1062e2008-11-26 10:26:38 +0000703static int gfs2_uevent(struct kset *kset, struct kobject *kobj,
704 struct kobj_uevent_env *env)
705{
706 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
Steven Whitehouse32e471e2011-05-10 15:01:59 +0100707 struct super_block *s = sdp->sd_vfs;
Steven Whitehouse02e3cc72009-02-10 13:48:30 +0000708
Steven Whitehousefdd1062e2008-11-26 10:26:38 +0000709 add_uevent_var(env, "LOCKTABLE=%s", sdp->sd_table_name);
710 add_uevent_var(env, "LOCKPROTO=%s", sdp->sd_proto_name);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100711 if (!test_bit(SDF_NOJOURNALID, &sdp->sd_flags))
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100712 add_uevent_var(env, "JOURNALID=%d", sdp->sd_lockstruct.ls_jid);
Christoph Hellwig85787092017-05-10 15:06:33 +0200713 if (!uuid_is_null(&s->s_uuid))
714 add_uevent_var(env, "UUID=%pUB", &s->s_uuid);
Steven Whitehousefdd1062e2008-11-26 10:26:38 +0000715 return 0;
716}
717
Emese Revfy9cd43612009-12-31 14:52:51 +0100718static const struct kset_uevent_ops gfs2_uevent_ops = {
Steven Whitehousefdd1062e2008-11-26 10:26:38 +0000719 .uevent = gfs2_uevent,
720};
721
David Teiglandb3b94fa2006-01-16 16:50:04 +0000722int gfs2_sys_init(void)
723{
Steven Whitehousefdd1062e2008-11-26 10:26:38 +0000724 gfs2_kset = kset_create_and_add("gfs2", &gfs2_uevent_ops, fs_kobj);
Greg Kroah-Hartman9bec1012007-10-29 20:13:17 +0100725 if (!gfs2_kset)
726 return -ENOMEM;
727 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000728}
729
730void gfs2_sys_uninit(void)
731{
Greg Kroah-Hartman9bec1012007-10-29 20:13:17 +0100732 kset_unregister(gfs2_kset);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000733}
734