blob: ca1f97ff898c951f495b555cf067203d3365ff43 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
Joe Perchesd77d1b52014-03-06 12:10:45 -080010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
David Teiglandb3b94fa2006-01-16 16:50:04 +000012#include <linux/sched.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010013#include <linux/cred.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000014#include <linux/spinlock.h>
15#include <linux/completion.h>
16#include <linux/buffer_head.h>
17#include <linux/module.h>
18#include <linux/kobject.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080019#include <linux/uaccess.h>
Steven Whitehousef057f6c2009-01-12 10:43:39 +000020#include <linux/gfs2_ondisk.h>
Steven Whitehouse31e54b02009-08-13 12:18:08 +010021#include <linux/genhd.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000022
23#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050024#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000025#include "sys.h"
26#include "super.h"
27#include "glock.h"
28#include "quota.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050029#include "util.h"
Steven Whitehouse64d576b2009-02-12 13:31:58 +000030#include "glops.h"
Tejun Heo6ecd7c22010-07-20 22:09:02 +020031#include "recovery.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000032
Steven Whitehouse48c2b612009-05-13 14:49:48 +010033struct gfs2_attr {
34 struct attribute attr;
35 ssize_t (*show)(struct gfs2_sbd *, char *);
36 ssize_t (*store)(struct gfs2_sbd *, const char *, size_t);
37};
38
39static ssize_t gfs2_attr_show(struct kobject *kobj, struct attribute *attr,
40 char *buf)
41{
42 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
43 struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
44 return a->show ? a->show(sdp, buf) : 0;
45}
46
47static ssize_t gfs2_attr_store(struct kobject *kobj, struct attribute *attr,
48 const char *buf, size_t len)
49{
50 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
51 struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
52 return a->store ? a->store(sdp, buf, len) : len;
53}
54
Emese Revfy52cf25d2010-01-19 02:58:23 +010055static const struct sysfs_ops gfs2_attr_ops = {
Steven Whitehouse48c2b612009-05-13 14:49:48 +010056 .show = gfs2_attr_show,
57 .store = gfs2_attr_store,
58};
59
60
61static struct kset *gfs2_kset;
62
David Teiglandb3b94fa2006-01-16 16:50:04 +000063static ssize_t id_show(struct gfs2_sbd *sdp, char *buf)
64{
Bob Petersonc7227e462007-11-02 09:37:15 -050065 return snprintf(buf, PAGE_SIZE, "%u:%u\n",
66 MAJOR(sdp->sd_vfs->s_dev), MINOR(sdp->sd_vfs->s_dev));
David Teiglandb3b94fa2006-01-16 16:50:04 +000067}
68
69static ssize_t fsname_show(struct gfs2_sbd *sdp, char *buf)
70{
David Teigland3204a6c2006-09-06 16:57:06 -050071 return snprintf(buf, PAGE_SIZE, "%s\n", sdp->sd_fsname);
David Teiglandb3b94fa2006-01-16 16:50:04 +000072}
73
Steven Whitehouse02e3cc72009-02-10 13:48:30 +000074static ssize_t uuid_show(struct gfs2_sbd *sdp, char *buf)
75{
Steven Whitehouse32e471e2011-05-10 15:01:59 +010076 struct super_block *s = sdp->sd_vfs;
Christoph Hellwig85787092017-05-10 15:06:33 +020077
Steven Whitehouse02e3cc72009-02-10 13:48:30 +000078 buf[0] = '\0';
Christoph Hellwig85787092017-05-10 15:06:33 +020079 if (uuid_is_null(&s->s_uuid))
Steven Whitehouse02e3cc72009-02-10 13:48:30 +000080 return 0;
Christoph Hellwig85787092017-05-10 15:06:33 +020081 return snprintf(buf, PAGE_SIZE, "%pUB\n", &s->s_uuid);
Steven Whitehouse02e3cc72009-02-10 13:48:30 +000082}
83
David Teiglandb3b94fa2006-01-16 16:50:04 +000084static ssize_t freeze_show(struct gfs2_sbd *sdp, char *buf)
85{
Steven Whitehoused564053f2013-01-11 10:49:34 +000086 struct super_block *sb = sdp->sd_vfs;
87 int frozen = (sb->s_writers.frozen == SB_UNFROZEN) ? 0 : 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +000088
alex chen3566c962015-01-12 19:01:03 +080089 return snprintf(buf, PAGE_SIZE, "%d\n", frozen);
David Teiglandb3b94fa2006-01-16 16:50:04 +000090}
91
92static ssize_t freeze_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
93{
Fabian Fredericke50ead42015-05-05 13:23:22 -050094 int error, n;
95
96 error = kstrtoint(buf, 0, &n);
97 if (error)
98 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +000099
100 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100101 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000102
103 switch (n) {
104 case 0:
Steven Whitehoused564053f2013-01-11 10:49:34 +0000105 error = thaw_super(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000106 break;
107 case 1:
Steven Whitehoused564053f2013-01-11 10:49:34 +0000108 error = freeze_super(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109 break;
110 default:
Steven Whitehoused564053f2013-01-11 10:49:34 +0000111 return -EINVAL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000112 }
113
Steven Whitehoused564053f2013-01-11 10:49:34 +0000114 if (error) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000115 fs_warn(sdp, "freeze %d error %d", n, error);
Steven Whitehoused564053f2013-01-11 10:49:34 +0000116 return error;
117 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000118
Steven Whitehoused564053f2013-01-11 10:49:34 +0000119 return len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000120}
121
122static ssize_t withdraw_show(struct gfs2_sbd *sdp, char *buf)
123{
124 unsigned int b = test_bit(SDF_SHUTDOWN, &sdp->sd_flags);
David Teigland3204a6c2006-09-06 16:57:06 -0500125 return snprintf(buf, PAGE_SIZE, "%u\n", b);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000126}
127
128static ssize_t withdraw_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
129{
Fabian Fredericke50ead42015-05-05 13:23:22 -0500130 int error, val;
131
David Teiglandb3b94fa2006-01-16 16:50:04 +0000132 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100133 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000134
Fabian Fredericke50ead42015-05-05 13:23:22 -0500135 error = kstrtoint(buf, 0, &val);
136 if (error)
137 return error;
138
139 if (val != 1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000140 return -EINVAL;
141
Joe Perchescb94eb02014-03-06 12:17:21 -0800142 gfs2_lm_withdraw(sdp, "withdrawing from cluster at user's request\n");
143
David Teiglandb3b94fa2006-01-16 16:50:04 +0000144 return len;
145}
146
147static ssize_t statfs_sync_store(struct gfs2_sbd *sdp, const char *buf,
148 size_t len)
149{
Fabian Fredericke50ead42015-05-05 13:23:22 -0500150 int error, val;
151
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100153 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000154
Fabian Fredericke50ead42015-05-05 13:23:22 -0500155 error = kstrtoint(buf, 0, &val);
156 if (error)
157 return error;
158
159 if (val != 1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000160 return -EINVAL;
161
Steven Whitehouse8c42d632009-09-11 14:36:44 +0100162 gfs2_statfs_sync(sdp->sd_vfs, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000163 return len;
164}
165
David Teiglandb3b94fa2006-01-16 16:50:04 +0000166static ssize_t quota_sync_store(struct gfs2_sbd *sdp, const char *buf,
167 size_t len)
168{
Fabian Fredericke50ead42015-05-05 13:23:22 -0500169 int error, val;
170
David Teiglandb3b94fa2006-01-16 16:50:04 +0000171 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100172 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000173
Fabian Fredericke50ead42015-05-05 13:23:22 -0500174 error = kstrtoint(buf, 0, &val);
175 if (error)
176 return error;
177
178 if (val != 1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000179 return -EINVAL;
180
Jan Karaceed1722012-07-03 16:45:28 +0200181 gfs2_quota_sync(sdp->sd_vfs, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000182 return len;
183}
184
185static ssize_t quota_refresh_user_store(struct gfs2_sbd *sdp, const char *buf,
186 size_t len)
187{
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800188 struct kqid qid;
Steven Whitehouseea762332009-09-15 16:20:30 +0100189 int error;
Steven Whitehousecd915492006-09-04 12:49:07 -0400190 u32 id;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000191
192 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100193 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000194
Fabian Fredericke50ead42015-05-05 13:23:22 -0500195 error = kstrtou32(buf, 0, &id);
196 if (error)
197 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000198
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800199 qid = make_kqid(current_user_ns(), USRQUOTA, id);
200 if (!qid_valid(qid))
201 return -EINVAL;
202
203 error = gfs2_quota_refresh(sdp, qid);
Steven Whitehouseea762332009-09-15 16:20:30 +0100204 return error ? error : len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000205}
206
207static ssize_t quota_refresh_group_store(struct gfs2_sbd *sdp, const char *buf,
208 size_t len)
209{
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800210 struct kqid qid;
Steven Whitehouseea762332009-09-15 16:20:30 +0100211 int error;
Steven Whitehousecd915492006-09-04 12:49:07 -0400212 u32 id;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213
214 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100215 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000216
Fabian Fredericke50ead42015-05-05 13:23:22 -0500217 error = kstrtou32(buf, 0, &id);
218 if (error)
219 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000220
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800221 qid = make_kqid(current_user_ns(), GRPQUOTA, id);
222 if (!qid_valid(qid))
223 return -EINVAL;
224
225 error = gfs2_quota_refresh(sdp, qid);
Steven Whitehouseea762332009-09-15 16:20:30 +0100226 return error ? error : len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000227}
228
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000229static ssize_t demote_rq_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
230{
231 struct gfs2_glock *gl;
232 const struct gfs2_glock_operations *glops;
233 unsigned int glmode;
234 unsigned int gltype;
235 unsigned long long glnum;
236 char mode[16];
237 int rv;
238
239 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100240 return -EPERM;
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000241
242 rv = sscanf(buf, "%u:%llu %15s", &gltype, &glnum,
243 mode);
244 if (rv != 3)
245 return -EINVAL;
246
247 if (strcmp(mode, "EX") == 0)
248 glmode = LM_ST_UNLOCKED;
249 else if ((strcmp(mode, "CW") == 0) || (strcmp(mode, "DF") == 0))
250 glmode = LM_ST_DEFERRED;
251 else if ((strcmp(mode, "PR") == 0) || (strcmp(mode, "SH") == 0))
252 glmode = LM_ST_SHARED;
253 else
254 return -EINVAL;
255
256 if (gltype > LM_TYPE_JOURNAL)
257 return -EINVAL;
Benjamin Marzinski24972552014-05-01 22:26:55 -0500258 if (gltype == LM_TYPE_NONDISK && glnum == GFS2_FREEZE_LOCK)
259 glops = &gfs2_freeze_glops;
Steven Whitehouse13466982010-10-06 09:58:44 +0100260 else
261 glops = gfs2_glops_list[gltype];
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000262 if (glops == NULL)
263 return -EINVAL;
Steven Whitehouse6a99be52010-05-14 14:05:51 +0100264 if (!test_and_set_bit(SDF_DEMOTE, &sdp->sd_flags))
Steven Whitehouse913a71d2010-05-06 11:03:29 +0100265 fs_info(sdp, "demote interface used\n");
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000266 rv = gfs2_glock_get(sdp, glnum, glops, 0, &gl);
267 if (rv)
268 return rv;
269 gfs2_glock_cb(gl, glmode);
270 gfs2_glock_put(gl);
271 return len;
272}
273
David Teiglandb3b94fa2006-01-16 16:50:04 +0000274
275#define GFS2_ATTR(name, mode, show, store) \
276static struct gfs2_attr gfs2_attr_##name = __ATTR(name, mode, show, store)
277
278GFS2_ATTR(id, 0444, id_show, NULL);
279GFS2_ATTR(fsname, 0444, fsname_show, NULL);
Steven Whitehouse02e3cc72009-02-10 13:48:30 +0000280GFS2_ATTR(uuid, 0444, uuid_show, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000281GFS2_ATTR(freeze, 0644, freeze_show, freeze_store);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000282GFS2_ATTR(withdraw, 0644, withdraw_show, withdraw_store);
283GFS2_ATTR(statfs_sync, 0200, NULL, statfs_sync_store);
284GFS2_ATTR(quota_sync, 0200, NULL, quota_sync_store);
285GFS2_ATTR(quota_refresh_user, 0200, NULL, quota_refresh_user_store);
286GFS2_ATTR(quota_refresh_group, 0200, NULL, quota_refresh_group_store);
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000287GFS2_ATTR(demote_rq, 0200, NULL, demote_rq_store);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000288
289static struct attribute *gfs2_attrs[] = {
290 &gfs2_attr_id.attr,
291 &gfs2_attr_fsname.attr,
Steven Whitehouse02e3cc72009-02-10 13:48:30 +0000292 &gfs2_attr_uuid.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000293 &gfs2_attr_freeze.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000294 &gfs2_attr_withdraw.attr,
295 &gfs2_attr_statfs_sync.attr,
296 &gfs2_attr_quota_sync.attr,
297 &gfs2_attr_quota_refresh_user.attr,
298 &gfs2_attr_quota_refresh_group.attr,
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000299 &gfs2_attr_demote_rq.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000300 NULL,
301};
302
Bob Peterson0d515212012-06-13 10:27:41 -0400303static void gfs2_sbd_release(struct kobject *kobj)
304{
305 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
306
307 kfree(sdp);
308}
309
David Teiglandb3b94fa2006-01-16 16:50:04 +0000310static struct kobj_type gfs2_ktype = {
Bob Peterson0d515212012-06-13 10:27:41 -0400311 .release = gfs2_sbd_release,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000312 .default_attrs = gfs2_attrs,
313 .sysfs_ops = &gfs2_attr_ops,
314};
315
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000316
317/*
318 * lock_module. Originally from lock_dlm
319 */
320
321static ssize_t proto_name_show(struct gfs2_sbd *sdp, char *buf)
322{
323 const struct lm_lockops *ops = sdp->sd_lockstruct.ls_ops;
324 return sprintf(buf, "%s\n", ops->lm_proto_name);
325}
326
327static ssize_t block_show(struct gfs2_sbd *sdp, char *buf)
328{
329 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
330 ssize_t ret;
331 int val = 0;
332
David Teiglande0c2a9a2012-01-09 17:18:05 -0500333 if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000334 val = 1;
335 ret = sprintf(buf, "%d\n", val);
336 return ret;
337}
338
339static ssize_t block_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
340{
341 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
Fabian Fredericke50ead42015-05-05 13:23:22 -0500342 int ret, val;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000343
Fabian Fredericke50ead42015-05-05 13:23:22 -0500344 ret = kstrtoint(buf, 0, &val);
345 if (ret)
346 return ret;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000347
348 if (val == 1)
David Teiglande0c2a9a2012-01-09 17:18:05 -0500349 set_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000350 else if (val == 0) {
David Teiglande0c2a9a2012-01-09 17:18:05 -0500351 clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100352 smp_mb__after_atomic();
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000353 gfs2_glock_thaw(sdp);
354 } else {
Fabian Fredericke50ead42015-05-05 13:23:22 -0500355 return -EINVAL;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000356 }
Fabian Fredericke50ead42015-05-05 13:23:22 -0500357 return len;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000358}
359
Steven Whitehousefd95e812013-02-13 12:21:40 +0000360static ssize_t wdack_show(struct gfs2_sbd *sdp, char *buf)
361{
362 int val = completion_done(&sdp->sd_wdack) ? 1 : 0;
363
364 return sprintf(buf, "%d\n", val);
365}
366
367static ssize_t wdack_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
368{
Fabian Fredericke50ead42015-05-05 13:23:22 -0500369 int ret, val;
Steven Whitehousefd95e812013-02-13 12:21:40 +0000370
Fabian Fredericke50ead42015-05-05 13:23:22 -0500371 ret = kstrtoint(buf, 0, &val);
372 if (ret)
373 return ret;
Steven Whitehousefd95e812013-02-13 12:21:40 +0000374
375 if ((val == 1) &&
376 !strcmp(sdp->sd_lockstruct.ls_ops->lm_proto_name, "lock_dlm"))
377 complete(&sdp->sd_wdack);
378 else
Fabian Fredericke50ead42015-05-05 13:23:22 -0500379 return -EINVAL;
380 return len;
Steven Whitehousefd95e812013-02-13 12:21:40 +0000381}
382
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000383static ssize_t lkfirst_show(struct gfs2_sbd *sdp, char *buf)
384{
385 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
386 return sprintf(buf, "%d\n", ls->ls_first);
387}
388
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100389static ssize_t lkfirst_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
390{
391 unsigned first;
392 int rv;
393
394 rv = sscanf(buf, "%u", &first);
395 if (rv != 1 || first > 1)
396 return -EINVAL;
Steven Whitehouse3942ae52011-07-11 08:53:30 +0100397 rv = wait_for_completion_killable(&sdp->sd_locking_init);
398 if (rv)
399 return rv;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100400 spin_lock(&sdp->sd_jindex_spin);
401 rv = -EBUSY;
402 if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
403 goto out;
404 rv = -EINVAL;
405 if (sdp->sd_args.ar_spectator)
406 goto out;
407 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
408 goto out;
David Teiglande0c2a9a2012-01-09 17:18:05 -0500409 sdp->sd_lockstruct.ls_first = first;
410 rv = 0;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100411out:
412 spin_unlock(&sdp->sd_jindex_spin);
413 return rv ? rv : len;
414}
415
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000416static ssize_t first_done_show(struct gfs2_sbd *sdp, char *buf)
417{
418 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
David Teiglande0c2a9a2012-01-09 17:18:05 -0500419 return sprintf(buf, "%d\n", !!test_bit(DFL_FIRST_MOUNT_DONE, &ls->ls_recover_flags));
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000420}
421
David Teiglande0c2a9a2012-01-09 17:18:05 -0500422int gfs2_recover_set(struct gfs2_sbd *sdp, unsigned jid)
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000423{
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000424 struct gfs2_jdesc *jd;
Steven Whitehousefe64d512009-05-19 10:01:18 +0100425 int rv;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000426
Bob Peterson0e48e0552014-06-02 09:40:25 -0400427 /* Wait for our primary journal to be initialized */
428 wait_for_completion(&sdp->sd_journal_ready);
429
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000430 spin_lock(&sdp->sd_jindex_spin);
Steven Whitehousefe64d512009-05-19 10:01:18 +0100431 rv = -EBUSY;
432 if (sdp->sd_jdesc->jd_jid == jid)
433 goto out;
434 rv = -ENOENT;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000435 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
436 if (jd->jd_jid != jid)
437 continue;
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200438 rv = gfs2_recover_journal(jd, false);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000439 break;
440 }
Steven Whitehousefe64d512009-05-19 10:01:18 +0100441out:
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000442 spin_unlock(&sdp->sd_jindex_spin);
David Teiglande0c2a9a2012-01-09 17:18:05 -0500443 return rv;
444}
445
446static ssize_t recover_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
447{
448 unsigned jid;
449 int rv;
450
451 rv = sscanf(buf, "%u", &jid);
452 if (rv != 1)
453 return -EINVAL;
454
David Teigland1a058f52012-05-01 15:50:48 -0500455 if (test_bit(SDF_NORECOVERY, &sdp->sd_flags)) {
456 rv = -ESHUTDOWN;
457 goto out;
458 }
David Teiglande0c2a9a2012-01-09 17:18:05 -0500459
David Teigland1a058f52012-05-01 15:50:48 -0500460 rv = gfs2_recover_set(sdp, jid);
461out:
Steven Whitehousefe64d512009-05-19 10:01:18 +0100462 return rv ? rv : len;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000463}
464
465static ssize_t recover_done_show(struct gfs2_sbd *sdp, char *buf)
466{
467 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
468 return sprintf(buf, "%d\n", ls->ls_recover_jid_done);
469}
470
471static ssize_t recover_status_show(struct gfs2_sbd *sdp, char *buf)
472{
473 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
474 return sprintf(buf, "%d\n", ls->ls_recover_jid_status);
475}
476
Steven Whitehousee1b28aa2009-05-26 15:41:27 +0100477static ssize_t jid_show(struct gfs2_sbd *sdp, char *buf)
478{
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100479 return sprintf(buf, "%d\n", sdp->sd_lockstruct.ls_jid);
Steven Whitehousee1b28aa2009-05-26 15:41:27 +0100480}
481
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100482static ssize_t jid_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
483{
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100484 int jid;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100485 int rv;
486
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100487 rv = sscanf(buf, "%d", &jid);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100488 if (rv != 1)
489 return -EINVAL;
Steven Whitehouse3942ae52011-07-11 08:53:30 +0100490 rv = wait_for_completion_killable(&sdp->sd_locking_init);
491 if (rv)
492 return rv;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100493 spin_lock(&sdp->sd_jindex_spin);
494 rv = -EINVAL;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100495 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
496 goto out;
497 rv = -EBUSY;
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100498 if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100499 goto out;
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100500 rv = 0;
501 if (sdp->sd_args.ar_spectator && jid > 0)
502 rv = jid = -EINVAL;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100503 sdp->sd_lockstruct.ls_jid = jid;
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100504 clear_bit(SDF_NOJOURNALID, &sdp->sd_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100505 smp_mb__after_atomic();
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100506 wake_up_bit(&sdp->sd_flags, SDF_NOJOURNALID);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100507out:
508 spin_unlock(&sdp->sd_jindex_spin);
509 return rv ? rv : len;
510}
511
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000512#define GDLM_ATTR(_name,_mode,_show,_store) \
Steven Whitehouse48c2b612009-05-13 14:49:48 +0100513static struct gfs2_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000514
Steven Whitehoused7e623d2009-08-11 11:20:11 +0100515GDLM_ATTR(proto_name, 0444, proto_name_show, NULL);
516GDLM_ATTR(block, 0644, block_show, block_store);
Steven Whitehousefd95e812013-02-13 12:21:40 +0000517GDLM_ATTR(withdraw, 0644, wdack_show, wdack_store);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100518GDLM_ATTR(jid, 0644, jid_show, jid_store);
519GDLM_ATTR(first, 0644, lkfirst_show, lkfirst_store);
Steven Whitehoused7e623d2009-08-11 11:20:11 +0100520GDLM_ATTR(first_done, 0444, first_done_show, NULL);
521GDLM_ATTR(recover, 0600, NULL, recover_store);
522GDLM_ATTR(recover_done, 0444, recover_done_show, NULL);
523GDLM_ATTR(recover_status, 0444, recover_status_show, NULL);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000524
525static struct attribute *lock_module_attrs[] = {
526 &gdlm_attr_proto_name.attr,
527 &gdlm_attr_block.attr,
528 &gdlm_attr_withdraw.attr,
Steven Whitehousee1b28aa2009-05-26 15:41:27 +0100529 &gdlm_attr_jid.attr,
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000530 &gdlm_attr_first.attr,
531 &gdlm_attr_first_done.attr,
532 &gdlm_attr_recover.attr,
533 &gdlm_attr_recover_done.attr,
534 &gdlm_attr_recover_status.attr,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400535 NULL,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000536};
537
David Teiglandb3b94fa2006-01-16 16:50:04 +0000538/*
David Teiglandb3b94fa2006-01-16 16:50:04 +0000539 * get and set struct gfs2_tune fields
540 */
541
542static ssize_t quota_scale_show(struct gfs2_sbd *sdp, char *buf)
543{
David Teigland3204a6c2006-09-06 16:57:06 -0500544 return snprintf(buf, PAGE_SIZE, "%u %u\n",
545 sdp->sd_tune.gt_quota_scale_num,
546 sdp->sd_tune.gt_quota_scale_den);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000547}
548
549static ssize_t quota_scale_store(struct gfs2_sbd *sdp, const char *buf,
550 size_t len)
551{
552 struct gfs2_tune *gt = &sdp->sd_tune;
553 unsigned int x, y;
554
555 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100556 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000557
558 if (sscanf(buf, "%u %u", &x, &y) != 2 || !y)
559 return -EINVAL;
560
561 spin_lock(&gt->gt_spin);
562 gt->gt_quota_scale_num = x;
563 gt->gt_quota_scale_den = y;
564 spin_unlock(&gt->gt_spin);
565 return len;
566}
567
568static ssize_t tune_set(struct gfs2_sbd *sdp, unsigned int *field,
569 int check_zero, const char *buf, size_t len)
570{
571 struct gfs2_tune *gt = &sdp->sd_tune;
572 unsigned int x;
Fabian Fredericke50ead42015-05-05 13:23:22 -0500573 int error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000574
575 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100576 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000577
Fabian Fredericke50ead42015-05-05 13:23:22 -0500578 error = kstrtouint(buf, 0, &x);
579 if (error)
580 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000581
582 if (check_zero && !x)
583 return -EINVAL;
584
585 spin_lock(&gt->gt_spin);
586 *field = x;
587 spin_unlock(&gt->gt_spin);
588 return len;
589}
590
David Teiglandb3b94fa2006-01-16 16:50:04 +0000591#define TUNE_ATTR_3(name, show, store) \
Steven Whitehouse48c2b612009-05-13 14:49:48 +0100592static struct gfs2_attr tune_attr_##name = __ATTR(name, 0644, show, store)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000593
594#define TUNE_ATTR_2(name, store) \
595static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf) \
596{ \
David Teigland3204a6c2006-09-06 16:57:06 -0500597 return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name); \
David Teiglandb3b94fa2006-01-16 16:50:04 +0000598} \
599TUNE_ATTR_3(name, name##_show, store)
600
601#define TUNE_ATTR(name, check_zero) \
602static ssize_t name##_store(struct gfs2_sbd *sdp, const char *buf, size_t len)\
603{ \
604 return tune_set(sdp, &sdp->sd_tune.gt_##name, check_zero, buf, len); \
605} \
606TUNE_ATTR_2(name, name##_store)
607
David Teiglandb3b94fa2006-01-16 16:50:04 +0000608TUNE_ATTR(quota_warn_period, 0);
609TUNE_ATTR(quota_quantum, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610TUNE_ATTR(max_readahead, 0);
611TUNE_ATTR(complain_secs, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612TUNE_ATTR(statfs_slow, 0);
613TUNE_ATTR(new_files_jdata, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000614TUNE_ATTR(statfs_quantum, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000615TUNE_ATTR_3(quota_scale, quota_scale_show, quota_scale_store);
616
617static struct attribute *tune_attrs[] = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000618 &tune_attr_quota_warn_period.attr,
619 &tune_attr_quota_quantum.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000620 &tune_attr_max_readahead.attr,
621 &tune_attr_complain_secs.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000622 &tune_attr_statfs_slow.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000623 &tune_attr_statfs_quantum.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000624 &tune_attr_quota_scale.attr,
625 &tune_attr_new_files_jdata.attr,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400626 NULL,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000627};
628
Arvind Yadav29695252017-06-30 08:33:54 -0500629static const struct attribute_group tune_group = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000630 .name = "tune",
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400631 .attrs = tune_attrs,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000632};
633
Arvind Yadav29695252017-06-30 08:33:54 -0500634static const struct attribute_group lock_module_group = {
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000635 .name = "lock_module",
636 .attrs = lock_module_attrs,
637};
638
David Teiglandb3b94fa2006-01-16 16:50:04 +0000639int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
640{
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100641 struct super_block *sb = sdp->sd_vfs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000642 int error;
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100643 char ro[20];
644 char spectator[20];
645 char *envp[] = { ro, spectator, NULL };
Bob Peterson0d515212012-06-13 10:27:41 -0400646 int sysfs_frees_sdp = 0;
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100647
648 sprintf(ro, "RDONLY=%d", (sb->s_flags & MS_RDONLY) ? 1 : 0);
649 sprintf(spectator, "SPECTATOR=%d", sdp->sd_args.ar_spectator ? 1 : 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650
Greg Kroah-Hartman9bec1012007-10-29 20:13:17 +0100651 sdp->sd_kobj.kset = gfs2_kset;
Greg Kroah-Hartman901195e2007-12-17 15:54:39 -0400652 error = kobject_init_and_add(&sdp->sd_kobj, &gfs2_ktype, NULL,
653 "%s", sdp->sd_table_name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000654 if (error)
Bob Peterson0d515212012-06-13 10:27:41 -0400655 goto fail_reg;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000656
Bob Peterson0d515212012-06-13 10:27:41 -0400657 sysfs_frees_sdp = 1; /* Freeing sdp is now done by sysfs calling
658 function gfs2_sbd_release. */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000659 error = sysfs_create_group(&sdp->sd_kobj, &tune_group);
660 if (error)
Steven Whitehousef6eb5342009-05-26 15:50:25 +0100661 goto fail_reg;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000662
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000663 error = sysfs_create_group(&sdp->sd_kobj, &lock_module_group);
664 if (error)
665 goto fail_tune;
666
Steven Whitehouse31e54b02009-08-13 12:18:08 +0100667 error = sysfs_create_link(&sdp->sd_kobj,
668 &disk_to_dev(sb->s_bdev->bd_disk)->kobj,
669 "device");
670 if (error)
671 goto fail_lock_module;
672
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100673 kobject_uevent_env(&sdp->sd_kobj, KOBJ_ADD, envp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000674 return 0;
675
Steven Whitehouse31e54b02009-08-13 12:18:08 +0100676fail_lock_module:
677 sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000678fail_tune:
679 sysfs_remove_group(&sdp->sd_kobj, &tune_group);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400680fail_reg:
Bob Peterson0d515212012-06-13 10:27:41 -0400681 free_percpu(sdp->sd_lkstats);
David Teigland65952fb2006-09-15 13:09:11 -0500682 fs_err(sdp, "error %d adding sysfs files", error);
Bob Peterson0d515212012-06-13 10:27:41 -0400683 if (sysfs_frees_sdp)
684 kobject_put(&sdp->sd_kobj);
685 else
686 kfree(sdp);
687 sb->s_fs_info = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000688 return error;
689}
690
691void gfs2_sys_fs_del(struct gfs2_sbd *sdp)
692{
Steven Whitehouse31e54b02009-08-13 12:18:08 +0100693 sysfs_remove_link(&sdp->sd_kobj, "device");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000694 sysfs_remove_group(&sdp->sd_kobj, &tune_group);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000695 sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800696 kobject_put(&sdp->sd_kobj);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697}
698
Steven Whitehousefdd10622008-11-26 10:26:38 +0000699static int gfs2_uevent(struct kset *kset, struct kobject *kobj,
700 struct kobj_uevent_env *env)
701{
702 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
Steven Whitehouse32e471e2011-05-10 15:01:59 +0100703 struct super_block *s = sdp->sd_vfs;
Steven Whitehouse02e3cc72009-02-10 13:48:30 +0000704
Steven Whitehousefdd10622008-11-26 10:26:38 +0000705 add_uevent_var(env, "LOCKTABLE=%s", sdp->sd_table_name);
706 add_uevent_var(env, "LOCKPROTO=%s", sdp->sd_proto_name);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100707 if (!test_bit(SDF_NOJOURNALID, &sdp->sd_flags))
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100708 add_uevent_var(env, "JOURNALID=%d", sdp->sd_lockstruct.ls_jid);
Christoph Hellwig85787092017-05-10 15:06:33 +0200709 if (!uuid_is_null(&s->s_uuid))
710 add_uevent_var(env, "UUID=%pUB", &s->s_uuid);
Steven Whitehousefdd10622008-11-26 10:26:38 +0000711 return 0;
712}
713
Emese Revfy9cd43612009-12-31 14:52:51 +0100714static const struct kset_uevent_ops gfs2_uevent_ops = {
Steven Whitehousefdd10622008-11-26 10:26:38 +0000715 .uevent = gfs2_uevent,
716};
717
David Teiglandb3b94fa2006-01-16 16:50:04 +0000718int gfs2_sys_init(void)
719{
Steven Whitehousefdd10622008-11-26 10:26:38 +0000720 gfs2_kset = kset_create_and_add("gfs2", &gfs2_uevent_ops, fs_kobj);
Greg Kroah-Hartman9bec1012007-10-29 20:13:17 +0100721 if (!gfs2_kset)
722 return -ENOMEM;
723 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000724}
725
726void gfs2_sys_uninit(void)
727{
Greg Kroah-Hartman9bec1012007-10-29 20:13:17 +0100728 kset_unregister(gfs2_kset);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729}
730