blob: c9ff1cf7d4f3ec9e4168d401ce595f9665c9e701 [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>
David Teiglandb3b94fa2006-01-16 16:50:04 +000013#include <linux/spinlock.h>
14#include <linux/completion.h>
15#include <linux/buffer_head.h>
16#include <linux/module.h>
17#include <linux/kobject.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000018#include <asm/uaccess.h>
Steven Whitehousef057f6c2009-01-12 10:43:39 +000019#include <linux/gfs2_ondisk.h>
Steven Whitehouse31e54b02009-08-13 12:18:08 +010020#include <linux/genhd.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000021
22#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050023#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000024#include "sys.h"
25#include "super.h"
26#include "glock.h"
27#include "quota.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050028#include "util.h"
Steven Whitehouse64d576b2009-02-12 13:31:58 +000029#include "glops.h"
Tejun Heo6ecd7c22010-07-20 22:09:02 +020030#include "recovery.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000031
Steven Whitehouse48c2b612009-05-13 14:49:48 +010032struct gfs2_attr {
33 struct attribute attr;
34 ssize_t (*show)(struct gfs2_sbd *, char *);
35 ssize_t (*store)(struct gfs2_sbd *, const char *, size_t);
36};
37
38static ssize_t gfs2_attr_show(struct kobject *kobj, struct attribute *attr,
39 char *buf)
40{
41 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
42 struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
43 return a->show ? a->show(sdp, buf) : 0;
44}
45
46static ssize_t gfs2_attr_store(struct kobject *kobj, struct attribute *attr,
47 const char *buf, size_t len)
48{
49 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
50 struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
51 return a->store ? a->store(sdp, buf, len) : len;
52}
53
Emese Revfy52cf25d2010-01-19 02:58:23 +010054static const struct sysfs_ops gfs2_attr_ops = {
Steven Whitehouse48c2b612009-05-13 14:49:48 +010055 .show = gfs2_attr_show,
56 .store = gfs2_attr_store,
57};
58
59
60static struct kset *gfs2_kset;
61
David Teiglandb3b94fa2006-01-16 16:50:04 +000062static ssize_t id_show(struct gfs2_sbd *sdp, char *buf)
63{
Bob Petersonc7227e462007-11-02 09:37:15 -050064 return snprintf(buf, PAGE_SIZE, "%u:%u\n",
65 MAJOR(sdp->sd_vfs->s_dev), MINOR(sdp->sd_vfs->s_dev));
David Teiglandb3b94fa2006-01-16 16:50:04 +000066}
67
68static ssize_t fsname_show(struct gfs2_sbd *sdp, char *buf)
69{
David Teigland3204a6c2006-09-06 16:57:06 -050070 return snprintf(buf, PAGE_SIZE, "%s\n", sdp->sd_fsname);
David Teiglandb3b94fa2006-01-16 16:50:04 +000071}
72
Steven Whitehouse02e3cc72009-02-10 13:48:30 +000073static int gfs2_uuid_valid(const u8 *uuid)
74{
75 int i;
76
77 for (i = 0; i < 16; i++) {
78 if (uuid[i])
79 return 1;
80 }
81 return 0;
82}
83
84static ssize_t uuid_show(struct gfs2_sbd *sdp, char *buf)
85{
Steven Whitehouse32e471e2011-05-10 15:01:59 +010086 struct super_block *s = sdp->sd_vfs;
87 const u8 *uuid = s->s_uuid;
Steven Whitehouse02e3cc72009-02-10 13:48:30 +000088 buf[0] = '\0';
89 if (!gfs2_uuid_valid(uuid))
90 return 0;
Joe Perchesf0b34ae2009-12-14 18:01:13 -080091 return snprintf(buf, PAGE_SIZE, "%pUB\n", uuid);
Steven Whitehouse02e3cc72009-02-10 13:48:30 +000092}
93
David Teiglandb3b94fa2006-01-16 16:50:04 +000094static ssize_t freeze_show(struct gfs2_sbd *sdp, char *buf)
95{
Steven Whitehoused564053f2013-01-11 10:49:34 +000096 struct super_block *sb = sdp->sd_vfs;
97 int frozen = (sb->s_writers.frozen == SB_UNFROZEN) ? 0 : 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +000098
alex chen3566c962015-01-12 19:01:03 +080099 return snprintf(buf, PAGE_SIZE, "%d\n", frozen);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000100}
101
102static ssize_t freeze_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
103{
Fabian Fredericke50ead42015-05-05 13:23:22 -0500104 int error, n;
105
106 error = kstrtoint(buf, 0, &n);
107 if (error)
108 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109
110 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100111 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000112
113 switch (n) {
114 case 0:
Steven Whitehoused564053f2013-01-11 10:49:34 +0000115 error = thaw_super(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000116 break;
117 case 1:
Steven Whitehoused564053f2013-01-11 10:49:34 +0000118 error = freeze_super(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000119 break;
120 default:
Steven Whitehoused564053f2013-01-11 10:49:34 +0000121 return -EINVAL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000122 }
123
Steven Whitehoused564053f2013-01-11 10:49:34 +0000124 if (error) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000125 fs_warn(sdp, "freeze %d error %d", n, error);
Steven Whitehoused564053f2013-01-11 10:49:34 +0000126 return error;
127 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000128
Steven Whitehoused564053f2013-01-11 10:49:34 +0000129 return len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000130}
131
132static ssize_t withdraw_show(struct gfs2_sbd *sdp, char *buf)
133{
134 unsigned int b = test_bit(SDF_SHUTDOWN, &sdp->sd_flags);
David Teigland3204a6c2006-09-06 16:57:06 -0500135 return snprintf(buf, PAGE_SIZE, "%u\n", b);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000136}
137
138static ssize_t withdraw_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
139{
Fabian Fredericke50ead42015-05-05 13:23:22 -0500140 int error, val;
141
David Teiglandb3b94fa2006-01-16 16:50:04 +0000142 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100143 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000144
Fabian Fredericke50ead42015-05-05 13:23:22 -0500145 error = kstrtoint(buf, 0, &val);
146 if (error)
147 return error;
148
149 if (val != 1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150 return -EINVAL;
151
Joe Perchescb94eb02014-03-06 12:17:21 -0800152 gfs2_lm_withdraw(sdp, "withdrawing from cluster at user's request\n");
153
David Teiglandb3b94fa2006-01-16 16:50:04 +0000154 return len;
155}
156
157static ssize_t statfs_sync_store(struct gfs2_sbd *sdp, const char *buf,
158 size_t len)
159{
Fabian Fredericke50ead42015-05-05 13:23:22 -0500160 int error, val;
161
David Teiglandb3b94fa2006-01-16 16:50:04 +0000162 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100163 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000164
Fabian Fredericke50ead42015-05-05 13:23:22 -0500165 error = kstrtoint(buf, 0, &val);
166 if (error)
167 return error;
168
169 if (val != 1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000170 return -EINVAL;
171
Steven Whitehouse8c42d632009-09-11 14:36:44 +0100172 gfs2_statfs_sync(sdp->sd_vfs, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000173 return len;
174}
175
David Teiglandb3b94fa2006-01-16 16:50:04 +0000176static ssize_t quota_sync_store(struct gfs2_sbd *sdp, const char *buf,
177 size_t len)
178{
Fabian Fredericke50ead42015-05-05 13:23:22 -0500179 int error, val;
180
David Teiglandb3b94fa2006-01-16 16:50:04 +0000181 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100182 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000183
Fabian Fredericke50ead42015-05-05 13:23:22 -0500184 error = kstrtoint(buf, 0, &val);
185 if (error)
186 return error;
187
188 if (val != 1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000189 return -EINVAL;
190
Jan Karaceed1722012-07-03 16:45:28 +0200191 gfs2_quota_sync(sdp->sd_vfs, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000192 return len;
193}
194
195static ssize_t quota_refresh_user_store(struct gfs2_sbd *sdp, const char *buf,
196 size_t len)
197{
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800198 struct kqid qid;
Steven Whitehouseea762332009-09-15 16:20:30 +0100199 int error;
Steven Whitehousecd915492006-09-04 12:49:07 -0400200 u32 id;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000201
202 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100203 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000204
Fabian Fredericke50ead42015-05-05 13:23:22 -0500205 error = kstrtou32(buf, 0, &id);
206 if (error)
207 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000208
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800209 qid = make_kqid(current_user_ns(), USRQUOTA, id);
210 if (!qid_valid(qid))
211 return -EINVAL;
212
213 error = gfs2_quota_refresh(sdp, qid);
Steven Whitehouseea762332009-09-15 16:20:30 +0100214 return error ? error : len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000215}
216
217static ssize_t quota_refresh_group_store(struct gfs2_sbd *sdp, const char *buf,
218 size_t len)
219{
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800220 struct kqid qid;
Steven Whitehouseea762332009-09-15 16:20:30 +0100221 int error;
Steven Whitehousecd915492006-09-04 12:49:07 -0400222 u32 id;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000223
224 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100225 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000226
Fabian Fredericke50ead42015-05-05 13:23:22 -0500227 error = kstrtou32(buf, 0, &id);
228 if (error)
229 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000230
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800231 qid = make_kqid(current_user_ns(), GRPQUOTA, id);
232 if (!qid_valid(qid))
233 return -EINVAL;
234
235 error = gfs2_quota_refresh(sdp, qid);
Steven Whitehouseea762332009-09-15 16:20:30 +0100236 return error ? error : len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000237}
238
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000239static ssize_t demote_rq_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
240{
241 struct gfs2_glock *gl;
242 const struct gfs2_glock_operations *glops;
243 unsigned int glmode;
244 unsigned int gltype;
245 unsigned long long glnum;
246 char mode[16];
247 int rv;
248
249 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100250 return -EPERM;
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000251
252 rv = sscanf(buf, "%u:%llu %15s", &gltype, &glnum,
253 mode);
254 if (rv != 3)
255 return -EINVAL;
256
257 if (strcmp(mode, "EX") == 0)
258 glmode = LM_ST_UNLOCKED;
259 else if ((strcmp(mode, "CW") == 0) || (strcmp(mode, "DF") == 0))
260 glmode = LM_ST_DEFERRED;
261 else if ((strcmp(mode, "PR") == 0) || (strcmp(mode, "SH") == 0))
262 glmode = LM_ST_SHARED;
263 else
264 return -EINVAL;
265
266 if (gltype > LM_TYPE_JOURNAL)
267 return -EINVAL;
Benjamin Marzinski24972552014-05-01 22:26:55 -0500268 if (gltype == LM_TYPE_NONDISK && glnum == GFS2_FREEZE_LOCK)
269 glops = &gfs2_freeze_glops;
Steven Whitehouse13466982010-10-06 09:58:44 +0100270 else
271 glops = gfs2_glops_list[gltype];
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000272 if (glops == NULL)
273 return -EINVAL;
Steven Whitehouse6a99be52010-05-14 14:05:51 +0100274 if (!test_and_set_bit(SDF_DEMOTE, &sdp->sd_flags))
Steven Whitehouse913a71d2010-05-06 11:03:29 +0100275 fs_info(sdp, "demote interface used\n");
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000276 rv = gfs2_glock_get(sdp, glnum, glops, 0, &gl);
277 if (rv)
278 return rv;
279 gfs2_glock_cb(gl, glmode);
280 gfs2_glock_put(gl);
281 return len;
282}
283
David Teiglandb3b94fa2006-01-16 16:50:04 +0000284
285#define GFS2_ATTR(name, mode, show, store) \
286static struct gfs2_attr gfs2_attr_##name = __ATTR(name, mode, show, store)
287
288GFS2_ATTR(id, 0444, id_show, NULL);
289GFS2_ATTR(fsname, 0444, fsname_show, NULL);
Steven Whitehouse02e3cc72009-02-10 13:48:30 +0000290GFS2_ATTR(uuid, 0444, uuid_show, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000291GFS2_ATTR(freeze, 0644, freeze_show, freeze_store);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000292GFS2_ATTR(withdraw, 0644, withdraw_show, withdraw_store);
293GFS2_ATTR(statfs_sync, 0200, NULL, statfs_sync_store);
294GFS2_ATTR(quota_sync, 0200, NULL, quota_sync_store);
295GFS2_ATTR(quota_refresh_user, 0200, NULL, quota_refresh_user_store);
296GFS2_ATTR(quota_refresh_group, 0200, NULL, quota_refresh_group_store);
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000297GFS2_ATTR(demote_rq, 0200, NULL, demote_rq_store);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000298
299static struct attribute *gfs2_attrs[] = {
300 &gfs2_attr_id.attr,
301 &gfs2_attr_fsname.attr,
Steven Whitehouse02e3cc72009-02-10 13:48:30 +0000302 &gfs2_attr_uuid.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000303 &gfs2_attr_freeze.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000304 &gfs2_attr_withdraw.attr,
305 &gfs2_attr_statfs_sync.attr,
306 &gfs2_attr_quota_sync.attr,
307 &gfs2_attr_quota_refresh_user.attr,
308 &gfs2_attr_quota_refresh_group.attr,
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000309 &gfs2_attr_demote_rq.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000310 NULL,
311};
312
Bob Peterson0d515212012-06-13 10:27:41 -0400313static void gfs2_sbd_release(struct kobject *kobj)
314{
315 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
316
317 kfree(sdp);
318}
319
David Teiglandb3b94fa2006-01-16 16:50:04 +0000320static struct kobj_type gfs2_ktype = {
Bob Peterson0d515212012-06-13 10:27:41 -0400321 .release = gfs2_sbd_release,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000322 .default_attrs = gfs2_attrs,
323 .sysfs_ops = &gfs2_attr_ops,
324};
325
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000326
327/*
328 * lock_module. Originally from lock_dlm
329 */
330
331static ssize_t proto_name_show(struct gfs2_sbd *sdp, char *buf)
332{
333 const struct lm_lockops *ops = sdp->sd_lockstruct.ls_ops;
334 return sprintf(buf, "%s\n", ops->lm_proto_name);
335}
336
337static ssize_t block_show(struct gfs2_sbd *sdp, char *buf)
338{
339 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
340 ssize_t ret;
341 int val = 0;
342
David Teiglande0c2a9a2012-01-09 17:18:05 -0500343 if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000344 val = 1;
345 ret = sprintf(buf, "%d\n", val);
346 return ret;
347}
348
349static ssize_t block_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
350{
351 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
Fabian Fredericke50ead42015-05-05 13:23:22 -0500352 int ret, val;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000353
Fabian Fredericke50ead42015-05-05 13:23:22 -0500354 ret = kstrtoint(buf, 0, &val);
355 if (ret)
356 return ret;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000357
358 if (val == 1)
David Teiglande0c2a9a2012-01-09 17:18:05 -0500359 set_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000360 else if (val == 0) {
David Teiglande0c2a9a2012-01-09 17:18:05 -0500361 clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100362 smp_mb__after_atomic();
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000363 gfs2_glock_thaw(sdp);
364 } else {
Fabian Fredericke50ead42015-05-05 13:23:22 -0500365 return -EINVAL;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000366 }
Fabian Fredericke50ead42015-05-05 13:23:22 -0500367 return len;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000368}
369
Steven Whitehousefd95e812013-02-13 12:21:40 +0000370static ssize_t wdack_show(struct gfs2_sbd *sdp, char *buf)
371{
372 int val = completion_done(&sdp->sd_wdack) ? 1 : 0;
373
374 return sprintf(buf, "%d\n", val);
375}
376
377static ssize_t wdack_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
378{
Fabian Fredericke50ead42015-05-05 13:23:22 -0500379 int ret, val;
Steven Whitehousefd95e812013-02-13 12:21:40 +0000380
Fabian Fredericke50ead42015-05-05 13:23:22 -0500381 ret = kstrtoint(buf, 0, &val);
382 if (ret)
383 return ret;
Steven Whitehousefd95e812013-02-13 12:21:40 +0000384
385 if ((val == 1) &&
386 !strcmp(sdp->sd_lockstruct.ls_ops->lm_proto_name, "lock_dlm"))
387 complete(&sdp->sd_wdack);
388 else
Fabian Fredericke50ead42015-05-05 13:23:22 -0500389 return -EINVAL;
390 return len;
Steven Whitehousefd95e812013-02-13 12:21:40 +0000391}
392
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000393static ssize_t lkfirst_show(struct gfs2_sbd *sdp, char *buf)
394{
395 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
396 return sprintf(buf, "%d\n", ls->ls_first);
397}
398
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100399static ssize_t lkfirst_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
400{
401 unsigned first;
402 int rv;
403
404 rv = sscanf(buf, "%u", &first);
405 if (rv != 1 || first > 1)
406 return -EINVAL;
Steven Whitehouse3942ae532011-07-11 08:53:30 +0100407 rv = wait_for_completion_killable(&sdp->sd_locking_init);
408 if (rv)
409 return rv;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100410 spin_lock(&sdp->sd_jindex_spin);
411 rv = -EBUSY;
412 if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
413 goto out;
414 rv = -EINVAL;
415 if (sdp->sd_args.ar_spectator)
416 goto out;
417 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
418 goto out;
David Teiglande0c2a9a2012-01-09 17:18:05 -0500419 sdp->sd_lockstruct.ls_first = first;
420 rv = 0;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100421out:
422 spin_unlock(&sdp->sd_jindex_spin);
423 return rv ? rv : len;
424}
425
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000426static ssize_t first_done_show(struct gfs2_sbd *sdp, char *buf)
427{
428 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
David Teiglande0c2a9a2012-01-09 17:18:05 -0500429 return sprintf(buf, "%d\n", !!test_bit(DFL_FIRST_MOUNT_DONE, &ls->ls_recover_flags));
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000430}
431
David Teiglande0c2a9a2012-01-09 17:18:05 -0500432int gfs2_recover_set(struct gfs2_sbd *sdp, unsigned jid)
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000433{
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000434 struct gfs2_jdesc *jd;
Steven Whitehousefe64d512009-05-19 10:01:18 +0100435 int rv;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000436
Bob Peterson0e48e0552014-06-02 09:40:25 -0400437 /* Wait for our primary journal to be initialized */
438 wait_for_completion(&sdp->sd_journal_ready);
439
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000440 spin_lock(&sdp->sd_jindex_spin);
Steven Whitehousefe64d512009-05-19 10:01:18 +0100441 rv = -EBUSY;
442 if (sdp->sd_jdesc->jd_jid == jid)
443 goto out;
444 rv = -ENOENT;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000445 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
446 if (jd->jd_jid != jid)
447 continue;
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200448 rv = gfs2_recover_journal(jd, false);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000449 break;
450 }
Steven Whitehousefe64d512009-05-19 10:01:18 +0100451out:
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000452 spin_unlock(&sdp->sd_jindex_spin);
David Teiglande0c2a9a2012-01-09 17:18:05 -0500453 return rv;
454}
455
456static ssize_t recover_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
457{
458 unsigned jid;
459 int rv;
460
461 rv = sscanf(buf, "%u", &jid);
462 if (rv != 1)
463 return -EINVAL;
464
David Teigland1a058f52012-05-01 15:50:48 -0500465 if (test_bit(SDF_NORECOVERY, &sdp->sd_flags)) {
466 rv = -ESHUTDOWN;
467 goto out;
468 }
David Teiglande0c2a9a2012-01-09 17:18:05 -0500469
David Teigland1a058f52012-05-01 15:50:48 -0500470 rv = gfs2_recover_set(sdp, jid);
471out:
Steven Whitehousefe64d512009-05-19 10:01:18 +0100472 return rv ? rv : len;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000473}
474
475static ssize_t recover_done_show(struct gfs2_sbd *sdp, char *buf)
476{
477 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
478 return sprintf(buf, "%d\n", ls->ls_recover_jid_done);
479}
480
481static ssize_t recover_status_show(struct gfs2_sbd *sdp, char *buf)
482{
483 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
484 return sprintf(buf, "%d\n", ls->ls_recover_jid_status);
485}
486
Steven Whitehousee1b28aa2009-05-26 15:41:27 +0100487static ssize_t jid_show(struct gfs2_sbd *sdp, char *buf)
488{
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100489 return sprintf(buf, "%d\n", sdp->sd_lockstruct.ls_jid);
Steven Whitehousee1b28aa2009-05-26 15:41:27 +0100490}
491
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100492static ssize_t jid_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
493{
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100494 int jid;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100495 int rv;
496
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100497 rv = sscanf(buf, "%d", &jid);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100498 if (rv != 1)
499 return -EINVAL;
Steven Whitehouse3942ae532011-07-11 08:53:30 +0100500 rv = wait_for_completion_killable(&sdp->sd_locking_init);
501 if (rv)
502 return rv;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100503 spin_lock(&sdp->sd_jindex_spin);
504 rv = -EINVAL;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100505 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
506 goto out;
507 rv = -EBUSY;
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100508 if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100509 goto out;
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100510 rv = 0;
511 if (sdp->sd_args.ar_spectator && jid > 0)
512 rv = jid = -EINVAL;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100513 sdp->sd_lockstruct.ls_jid = jid;
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100514 clear_bit(SDF_NOJOURNALID, &sdp->sd_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100515 smp_mb__after_atomic();
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100516 wake_up_bit(&sdp->sd_flags, SDF_NOJOURNALID);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100517out:
518 spin_unlock(&sdp->sd_jindex_spin);
519 return rv ? rv : len;
520}
521
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000522#define GDLM_ATTR(_name,_mode,_show,_store) \
Steven Whitehouse48c2b612009-05-13 14:49:48 +0100523static struct gfs2_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000524
Steven Whitehoused7e623d2009-08-11 11:20:11 +0100525GDLM_ATTR(proto_name, 0444, proto_name_show, NULL);
526GDLM_ATTR(block, 0644, block_show, block_store);
Steven Whitehousefd95e812013-02-13 12:21:40 +0000527GDLM_ATTR(withdraw, 0644, wdack_show, wdack_store);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100528GDLM_ATTR(jid, 0644, jid_show, jid_store);
529GDLM_ATTR(first, 0644, lkfirst_show, lkfirst_store);
Steven Whitehoused7e623d2009-08-11 11:20:11 +0100530GDLM_ATTR(first_done, 0444, first_done_show, NULL);
531GDLM_ATTR(recover, 0600, NULL, recover_store);
532GDLM_ATTR(recover_done, 0444, recover_done_show, NULL);
533GDLM_ATTR(recover_status, 0444, recover_status_show, NULL);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000534
535static struct attribute *lock_module_attrs[] = {
536 &gdlm_attr_proto_name.attr,
537 &gdlm_attr_block.attr,
538 &gdlm_attr_withdraw.attr,
Steven Whitehousee1b28aa2009-05-26 15:41:27 +0100539 &gdlm_attr_jid.attr,
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000540 &gdlm_attr_first.attr,
541 &gdlm_attr_first_done.attr,
542 &gdlm_attr_recover.attr,
543 &gdlm_attr_recover_done.attr,
544 &gdlm_attr_recover_status.attr,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400545 NULL,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000546};
547
David Teiglandb3b94fa2006-01-16 16:50:04 +0000548/*
David Teiglandb3b94fa2006-01-16 16:50:04 +0000549 * get and set struct gfs2_tune fields
550 */
551
552static ssize_t quota_scale_show(struct gfs2_sbd *sdp, char *buf)
553{
David Teigland3204a6c2006-09-06 16:57:06 -0500554 return snprintf(buf, PAGE_SIZE, "%u %u\n",
555 sdp->sd_tune.gt_quota_scale_num,
556 sdp->sd_tune.gt_quota_scale_den);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000557}
558
559static ssize_t quota_scale_store(struct gfs2_sbd *sdp, const char *buf,
560 size_t len)
561{
562 struct gfs2_tune *gt = &sdp->sd_tune;
563 unsigned int x, y;
564
565 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100566 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000567
568 if (sscanf(buf, "%u %u", &x, &y) != 2 || !y)
569 return -EINVAL;
570
571 spin_lock(&gt->gt_spin);
572 gt->gt_quota_scale_num = x;
573 gt->gt_quota_scale_den = y;
574 spin_unlock(&gt->gt_spin);
575 return len;
576}
577
578static ssize_t tune_set(struct gfs2_sbd *sdp, unsigned int *field,
579 int check_zero, const char *buf, size_t len)
580{
581 struct gfs2_tune *gt = &sdp->sd_tune;
582 unsigned int x;
Fabian Fredericke50ead42015-05-05 13:23:22 -0500583 int error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000584
585 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100586 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587
Fabian Fredericke50ead42015-05-05 13:23:22 -0500588 error = kstrtouint(buf, 0, &x);
589 if (error)
590 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000591
592 if (check_zero && !x)
593 return -EINVAL;
594
595 spin_lock(&gt->gt_spin);
596 *field = x;
597 spin_unlock(&gt->gt_spin);
598 return len;
599}
600
David Teiglandb3b94fa2006-01-16 16:50:04 +0000601#define TUNE_ATTR_3(name, show, store) \
Steven Whitehouse48c2b612009-05-13 14:49:48 +0100602static struct gfs2_attr tune_attr_##name = __ATTR(name, 0644, show, store)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000603
604#define TUNE_ATTR_2(name, store) \
605static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf) \
606{ \
David Teigland3204a6c2006-09-06 16:57:06 -0500607 return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name); \
David Teiglandb3b94fa2006-01-16 16:50:04 +0000608} \
609TUNE_ATTR_3(name, name##_show, store)
610
611#define TUNE_ATTR(name, check_zero) \
612static ssize_t name##_store(struct gfs2_sbd *sdp, const char *buf, size_t len)\
613{ \
614 return tune_set(sdp, &sdp->sd_tune.gt_##name, check_zero, buf, len); \
615} \
616TUNE_ATTR_2(name, name##_store)
617
David Teiglandb3b94fa2006-01-16 16:50:04 +0000618TUNE_ATTR(quota_warn_period, 0);
619TUNE_ATTR(quota_quantum, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000620TUNE_ATTR(max_readahead, 0);
621TUNE_ATTR(complain_secs, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000622TUNE_ATTR(statfs_slow, 0);
623TUNE_ATTR(new_files_jdata, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000624TUNE_ATTR(statfs_quantum, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000625TUNE_ATTR_3(quota_scale, quota_scale_show, quota_scale_store);
626
627static struct attribute *tune_attrs[] = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000628 &tune_attr_quota_warn_period.attr,
629 &tune_attr_quota_quantum.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000630 &tune_attr_max_readahead.attr,
631 &tune_attr_complain_secs.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000632 &tune_attr_statfs_slow.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000633 &tune_attr_statfs_quantum.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000634 &tune_attr_quota_scale.attr,
635 &tune_attr_new_files_jdata.attr,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400636 NULL,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000637};
638
David Teiglandb3b94fa2006-01-16 16:50:04 +0000639static struct attribute_group tune_group = {
640 .name = "tune",
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400641 .attrs = tune_attrs,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000642};
643
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000644static struct attribute_group lock_module_group = {
645 .name = "lock_module",
646 .attrs = lock_module_attrs,
647};
648
David Teiglandb3b94fa2006-01-16 16:50:04 +0000649int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
650{
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100651 struct super_block *sb = sdp->sd_vfs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000652 int error;
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100653 char ro[20];
654 char spectator[20];
655 char *envp[] = { ro, spectator, NULL };
Bob Peterson0d515212012-06-13 10:27:41 -0400656 int sysfs_frees_sdp = 0;
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100657
658 sprintf(ro, "RDONLY=%d", (sb->s_flags & MS_RDONLY) ? 1 : 0);
659 sprintf(spectator, "SPECTATOR=%d", sdp->sd_args.ar_spectator ? 1 : 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000660
Greg Kroah-Hartman9bec1012007-10-29 20:13:17 +0100661 sdp->sd_kobj.kset = gfs2_kset;
Greg Kroah-Hartman901195e2007-12-17 15:54:39 -0400662 error = kobject_init_and_add(&sdp->sd_kobj, &gfs2_ktype, NULL,
663 "%s", sdp->sd_table_name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000664 if (error)
Bob Peterson0d515212012-06-13 10:27:41 -0400665 goto fail_reg;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000666
Bob Peterson0d515212012-06-13 10:27:41 -0400667 sysfs_frees_sdp = 1; /* Freeing sdp is now done by sysfs calling
668 function gfs2_sbd_release. */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000669 error = sysfs_create_group(&sdp->sd_kobj, &tune_group);
670 if (error)
Steven Whitehousef6eb5342009-05-26 15:50:25 +0100671 goto fail_reg;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000672
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000673 error = sysfs_create_group(&sdp->sd_kobj, &lock_module_group);
674 if (error)
675 goto fail_tune;
676
Steven Whitehouse31e54b02009-08-13 12:18:08 +0100677 error = sysfs_create_link(&sdp->sd_kobj,
678 &disk_to_dev(sb->s_bdev->bd_disk)->kobj,
679 "device");
680 if (error)
681 goto fail_lock_module;
682
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100683 kobject_uevent_env(&sdp->sd_kobj, KOBJ_ADD, envp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000684 return 0;
685
Steven Whitehouse31e54b02009-08-13 12:18:08 +0100686fail_lock_module:
687 sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000688fail_tune:
689 sysfs_remove_group(&sdp->sd_kobj, &tune_group);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400690fail_reg:
Bob Peterson0d515212012-06-13 10:27:41 -0400691 free_percpu(sdp->sd_lkstats);
David Teigland65952fb2006-09-15 13:09:11 -0500692 fs_err(sdp, "error %d adding sysfs files", error);
Bob Peterson0d515212012-06-13 10:27:41 -0400693 if (sysfs_frees_sdp)
694 kobject_put(&sdp->sd_kobj);
695 else
696 kfree(sdp);
697 sb->s_fs_info = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000698 return error;
699}
700
701void gfs2_sys_fs_del(struct gfs2_sbd *sdp)
702{
Steven Whitehouse31e54b02009-08-13 12:18:08 +0100703 sysfs_remove_link(&sdp->sd_kobj, "device");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000704 sysfs_remove_group(&sdp->sd_kobj, &tune_group);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000705 sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800706 kobject_put(&sdp->sd_kobj);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707}
708
Steven Whitehousefdd1062e2008-11-26 10:26:38 +0000709static int gfs2_uevent(struct kset *kset, struct kobject *kobj,
710 struct kobj_uevent_env *env)
711{
712 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
Steven Whitehouse32e471e2011-05-10 15:01:59 +0100713 struct super_block *s = sdp->sd_vfs;
714 const u8 *uuid = s->s_uuid;
Steven Whitehouse02e3cc72009-02-10 13:48:30 +0000715
Steven Whitehousefdd1062e2008-11-26 10:26:38 +0000716 add_uevent_var(env, "LOCKTABLE=%s", sdp->sd_table_name);
717 add_uevent_var(env, "LOCKPROTO=%s", sdp->sd_proto_name);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100718 if (!test_bit(SDF_NOJOURNALID, &sdp->sd_flags))
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100719 add_uevent_var(env, "JOURNALID=%d", sdp->sd_lockstruct.ls_jid);
Joe Perchesf0b34ae2009-12-14 18:01:13 -0800720 if (gfs2_uuid_valid(uuid))
721 add_uevent_var(env, "UUID=%pUB", uuid);
Steven Whitehousefdd1062e2008-11-26 10:26:38 +0000722 return 0;
723}
724
Emese Revfy9cd43612009-12-31 14:52:51 +0100725static const struct kset_uevent_ops gfs2_uevent_ops = {
Steven Whitehousefdd1062e2008-11-26 10:26:38 +0000726 .uevent = gfs2_uevent,
727};
728
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729int gfs2_sys_init(void)
730{
Steven Whitehousefdd1062e2008-11-26 10:26:38 +0000731 gfs2_kset = kset_create_and_add("gfs2", &gfs2_uevent_ops, fs_kobj);
Greg Kroah-Hartman9bec1012007-10-29 20:13:17 +0100732 if (!gfs2_kset)
733 return -ENOMEM;
734 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000735}
736
737void gfs2_sys_uninit(void)
738{
Greg Kroah-Hartman9bec1012007-10-29 20:13:17 +0100739 kset_unregister(gfs2_kset);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000740}
741