blob: 3ab566ba5696eb73db835f237a26a4334a4dbccc [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
Steven Whitehoused564053f2013-01-11 10:49:34 +000099 return snprintf(buf, PAGE_SIZE, "%u\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{
Steven Whitehoused564053f2013-01-11 10:49:34 +0000104 int error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000105 int n = simple_strtol(buf, NULL, 0);
106
107 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100108 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109
110 switch (n) {
111 case 0:
Steven Whitehoused564053f2013-01-11 10:49:34 +0000112 error = thaw_super(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000113 break;
114 case 1:
Steven Whitehoused564053f2013-01-11 10:49:34 +0000115 error = freeze_super(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000116 break;
117 default:
Steven Whitehoused564053f2013-01-11 10:49:34 +0000118 return -EINVAL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000119 }
120
Steven Whitehoused564053f2013-01-11 10:49:34 +0000121 if (error) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000122 fs_warn(sdp, "freeze %d error %d", n, error);
Steven Whitehoused564053f2013-01-11 10:49:34 +0000123 return error;
124 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000125
Steven Whitehoused564053f2013-01-11 10:49:34 +0000126 return len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000127}
128
129static ssize_t withdraw_show(struct gfs2_sbd *sdp, char *buf)
130{
131 unsigned int b = test_bit(SDF_SHUTDOWN, &sdp->sd_flags);
David Teigland3204a6c2006-09-06 16:57:06 -0500132 return snprintf(buf, PAGE_SIZE, "%u\n", b);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000133}
134
135static ssize_t withdraw_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
136{
137 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100138 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000139
140 if (simple_strtol(buf, NULL, 0) != 1)
141 return -EINVAL;
142
Joe Perchescb94eb02014-03-06 12:17:21 -0800143 gfs2_lm_withdraw(sdp, "withdrawing from cluster at user's request\n");
144
David Teiglandb3b94fa2006-01-16 16:50:04 +0000145 return len;
146}
147
148static ssize_t statfs_sync_store(struct gfs2_sbd *sdp, const char *buf,
149 size_t len)
150{
151 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100152 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000153
154 if (simple_strtol(buf, NULL, 0) != 1)
155 return -EINVAL;
156
Steven Whitehouse8c42d632009-09-11 14:36:44 +0100157 gfs2_statfs_sync(sdp->sd_vfs, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000158 return len;
159}
160
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161static ssize_t quota_sync_store(struct gfs2_sbd *sdp, const char *buf,
162 size_t len)
163{
164 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100165 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000166
167 if (simple_strtol(buf, NULL, 0) != 1)
168 return -EINVAL;
169
Jan Karaceed1722012-07-03 16:45:28 +0200170 gfs2_quota_sync(sdp->sd_vfs, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000171 return len;
172}
173
174static ssize_t quota_refresh_user_store(struct gfs2_sbd *sdp, const char *buf,
175 size_t len)
176{
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800177 struct kqid qid;
Steven Whitehouseea762332009-09-15 16:20:30 +0100178 int error;
Steven Whitehousecd915492006-09-04 12:49:07 -0400179 u32 id;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000180
181 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100182 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000183
184 id = simple_strtoul(buf, NULL, 0);
185
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800186 qid = make_kqid(current_user_ns(), USRQUOTA, id);
187 if (!qid_valid(qid))
188 return -EINVAL;
189
190 error = gfs2_quota_refresh(sdp, qid);
Steven Whitehouseea762332009-09-15 16:20:30 +0100191 return error ? error : len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000192}
193
194static ssize_t quota_refresh_group_store(struct gfs2_sbd *sdp, const char *buf,
195 size_t len)
196{
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800197 struct kqid qid;
Steven Whitehouseea762332009-09-15 16:20:30 +0100198 int error;
Steven Whitehousecd915492006-09-04 12:49:07 -0400199 u32 id;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000200
201 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100202 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000203
204 id = simple_strtoul(buf, NULL, 0);
205
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800206 qid = make_kqid(current_user_ns(), GRPQUOTA, id);
207 if (!qid_valid(qid))
208 return -EINVAL;
209
210 error = gfs2_quota_refresh(sdp, qid);
Steven Whitehouseea762332009-09-15 16:20:30 +0100211 return error ? error : len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212}
213
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000214static ssize_t demote_rq_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
215{
216 struct gfs2_glock *gl;
217 const struct gfs2_glock_operations *glops;
218 unsigned int glmode;
219 unsigned int gltype;
220 unsigned long long glnum;
221 char mode[16];
222 int rv;
223
224 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100225 return -EPERM;
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000226
227 rv = sscanf(buf, "%u:%llu %15s", &gltype, &glnum,
228 mode);
229 if (rv != 3)
230 return -EINVAL;
231
232 if (strcmp(mode, "EX") == 0)
233 glmode = LM_ST_UNLOCKED;
234 else if ((strcmp(mode, "CW") == 0) || (strcmp(mode, "DF") == 0))
235 glmode = LM_ST_DEFERRED;
236 else if ((strcmp(mode, "PR") == 0) || (strcmp(mode, "SH") == 0))
237 glmode = LM_ST_SHARED;
238 else
239 return -EINVAL;
240
241 if (gltype > LM_TYPE_JOURNAL)
242 return -EINVAL;
Benjamin Marzinski24972552014-05-01 22:26:55 -0500243 if (gltype == LM_TYPE_NONDISK && glnum == GFS2_FREEZE_LOCK)
244 glops = &gfs2_freeze_glops;
Steven Whitehouse13466982010-10-06 09:58:44 +0100245 else
246 glops = gfs2_glops_list[gltype];
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000247 if (glops == NULL)
248 return -EINVAL;
Steven Whitehouse6a99be52010-05-14 14:05:51 +0100249 if (!test_and_set_bit(SDF_DEMOTE, &sdp->sd_flags))
Steven Whitehouse913a71d2010-05-06 11:03:29 +0100250 fs_info(sdp, "demote interface used\n");
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000251 rv = gfs2_glock_get(sdp, glnum, glops, 0, &gl);
252 if (rv)
253 return rv;
254 gfs2_glock_cb(gl, glmode);
255 gfs2_glock_put(gl);
256 return len;
257}
258
David Teiglandb3b94fa2006-01-16 16:50:04 +0000259
260#define GFS2_ATTR(name, mode, show, store) \
261static struct gfs2_attr gfs2_attr_##name = __ATTR(name, mode, show, store)
262
263GFS2_ATTR(id, 0444, id_show, NULL);
264GFS2_ATTR(fsname, 0444, fsname_show, NULL);
Steven Whitehouse02e3cc72009-02-10 13:48:30 +0000265GFS2_ATTR(uuid, 0444, uuid_show, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000266GFS2_ATTR(freeze, 0644, freeze_show, freeze_store);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000267GFS2_ATTR(withdraw, 0644, withdraw_show, withdraw_store);
268GFS2_ATTR(statfs_sync, 0200, NULL, statfs_sync_store);
269GFS2_ATTR(quota_sync, 0200, NULL, quota_sync_store);
270GFS2_ATTR(quota_refresh_user, 0200, NULL, quota_refresh_user_store);
271GFS2_ATTR(quota_refresh_group, 0200, NULL, quota_refresh_group_store);
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000272GFS2_ATTR(demote_rq, 0200, NULL, demote_rq_store);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000273
274static struct attribute *gfs2_attrs[] = {
275 &gfs2_attr_id.attr,
276 &gfs2_attr_fsname.attr,
Steven Whitehouse02e3cc72009-02-10 13:48:30 +0000277 &gfs2_attr_uuid.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000278 &gfs2_attr_freeze.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000279 &gfs2_attr_withdraw.attr,
280 &gfs2_attr_statfs_sync.attr,
281 &gfs2_attr_quota_sync.attr,
282 &gfs2_attr_quota_refresh_user.attr,
283 &gfs2_attr_quota_refresh_group.attr,
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000284 &gfs2_attr_demote_rq.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000285 NULL,
286};
287
Bob Peterson0d515212012-06-13 10:27:41 -0400288static void gfs2_sbd_release(struct kobject *kobj)
289{
290 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
291
292 kfree(sdp);
293}
294
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295static struct kobj_type gfs2_ktype = {
Bob Peterson0d515212012-06-13 10:27:41 -0400296 .release = gfs2_sbd_release,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000297 .default_attrs = gfs2_attrs,
298 .sysfs_ops = &gfs2_attr_ops,
299};
300
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000301
302/*
303 * lock_module. Originally from lock_dlm
304 */
305
306static ssize_t proto_name_show(struct gfs2_sbd *sdp, char *buf)
307{
308 const struct lm_lockops *ops = sdp->sd_lockstruct.ls_ops;
309 return sprintf(buf, "%s\n", ops->lm_proto_name);
310}
311
312static ssize_t block_show(struct gfs2_sbd *sdp, char *buf)
313{
314 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
315 ssize_t ret;
316 int val = 0;
317
David Teiglande0c2a9a2012-01-09 17:18:05 -0500318 if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000319 val = 1;
320 ret = sprintf(buf, "%d\n", val);
321 return ret;
322}
323
324static ssize_t block_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
325{
326 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
327 ssize_t ret = len;
328 int val;
329
330 val = simple_strtol(buf, NULL, 0);
331
332 if (val == 1)
David Teiglande0c2a9a2012-01-09 17:18:05 -0500333 set_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000334 else if (val == 0) {
David Teiglande0c2a9a2012-01-09 17:18:05 -0500335 clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100336 smp_mb__after_atomic();
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000337 gfs2_glock_thaw(sdp);
338 } else {
339 ret = -EINVAL;
340 }
341 return ret;
342}
343
Steven Whitehousefd95e812013-02-13 12:21:40 +0000344static ssize_t wdack_show(struct gfs2_sbd *sdp, char *buf)
345{
346 int val = completion_done(&sdp->sd_wdack) ? 1 : 0;
347
348 return sprintf(buf, "%d\n", val);
349}
350
351static ssize_t wdack_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
352{
353 ssize_t ret = len;
354 int val;
355
356 val = simple_strtol(buf, NULL, 0);
357
358 if ((val == 1) &&
359 !strcmp(sdp->sd_lockstruct.ls_ops->lm_proto_name, "lock_dlm"))
360 complete(&sdp->sd_wdack);
361 else
362 ret = -EINVAL;
363 return ret;
364}
365
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000366static ssize_t lkfirst_show(struct gfs2_sbd *sdp, char *buf)
367{
368 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
369 return sprintf(buf, "%d\n", ls->ls_first);
370}
371
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100372static ssize_t lkfirst_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
373{
374 unsigned first;
375 int rv;
376
377 rv = sscanf(buf, "%u", &first);
378 if (rv != 1 || first > 1)
379 return -EINVAL;
Steven Whitehouse3942ae52011-07-11 08:53:30 +0100380 rv = wait_for_completion_killable(&sdp->sd_locking_init);
381 if (rv)
382 return rv;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100383 spin_lock(&sdp->sd_jindex_spin);
384 rv = -EBUSY;
385 if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
386 goto out;
387 rv = -EINVAL;
388 if (sdp->sd_args.ar_spectator)
389 goto out;
390 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
391 goto out;
David Teiglande0c2a9a2012-01-09 17:18:05 -0500392 sdp->sd_lockstruct.ls_first = first;
393 rv = 0;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100394out:
395 spin_unlock(&sdp->sd_jindex_spin);
396 return rv ? rv : len;
397}
398
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000399static ssize_t first_done_show(struct gfs2_sbd *sdp, char *buf)
400{
401 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
David Teiglande0c2a9a2012-01-09 17:18:05 -0500402 return sprintf(buf, "%d\n", !!test_bit(DFL_FIRST_MOUNT_DONE, &ls->ls_recover_flags));
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000403}
404
David Teiglande0c2a9a2012-01-09 17:18:05 -0500405int gfs2_recover_set(struct gfs2_sbd *sdp, unsigned jid)
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000406{
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000407 struct gfs2_jdesc *jd;
Steven Whitehousefe64d512009-05-19 10:01:18 +0100408 int rv;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000409
Bob Peterson0e48e0552014-06-02 09:40:25 -0400410 /* Wait for our primary journal to be initialized */
411 wait_for_completion(&sdp->sd_journal_ready);
412
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000413 spin_lock(&sdp->sd_jindex_spin);
Steven Whitehousefe64d512009-05-19 10:01:18 +0100414 rv = -EBUSY;
415 if (sdp->sd_jdesc->jd_jid == jid)
416 goto out;
417 rv = -ENOENT;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000418 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
419 if (jd->jd_jid != jid)
420 continue;
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200421 rv = gfs2_recover_journal(jd, false);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000422 break;
423 }
Steven Whitehousefe64d512009-05-19 10:01:18 +0100424out:
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000425 spin_unlock(&sdp->sd_jindex_spin);
David Teiglande0c2a9a2012-01-09 17:18:05 -0500426 return rv;
427}
428
429static ssize_t recover_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
430{
431 unsigned jid;
432 int rv;
433
434 rv = sscanf(buf, "%u", &jid);
435 if (rv != 1)
436 return -EINVAL;
437
David Teigland1a058f52012-05-01 15:50:48 -0500438 if (test_bit(SDF_NORECOVERY, &sdp->sd_flags)) {
439 rv = -ESHUTDOWN;
440 goto out;
441 }
David Teiglande0c2a9a2012-01-09 17:18:05 -0500442
David Teigland1a058f52012-05-01 15:50:48 -0500443 rv = gfs2_recover_set(sdp, jid);
444out:
Steven Whitehousefe64d512009-05-19 10:01:18 +0100445 return rv ? rv : len;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000446}
447
448static ssize_t recover_done_show(struct gfs2_sbd *sdp, char *buf)
449{
450 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
451 return sprintf(buf, "%d\n", ls->ls_recover_jid_done);
452}
453
454static ssize_t recover_status_show(struct gfs2_sbd *sdp, char *buf)
455{
456 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
457 return sprintf(buf, "%d\n", ls->ls_recover_jid_status);
458}
459
Steven Whitehousee1b28aa2009-05-26 15:41:27 +0100460static ssize_t jid_show(struct gfs2_sbd *sdp, char *buf)
461{
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100462 return sprintf(buf, "%d\n", sdp->sd_lockstruct.ls_jid);
Steven Whitehousee1b28aa2009-05-26 15:41:27 +0100463}
464
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100465static ssize_t jid_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
466{
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100467 int jid;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100468 int rv;
469
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100470 rv = sscanf(buf, "%d", &jid);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100471 if (rv != 1)
472 return -EINVAL;
Steven Whitehouse3942ae52011-07-11 08:53:30 +0100473 rv = wait_for_completion_killable(&sdp->sd_locking_init);
474 if (rv)
475 return rv;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100476 spin_lock(&sdp->sd_jindex_spin);
477 rv = -EINVAL;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100478 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
479 goto out;
480 rv = -EBUSY;
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100481 if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100482 goto out;
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100483 rv = 0;
484 if (sdp->sd_args.ar_spectator && jid > 0)
485 rv = jid = -EINVAL;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100486 sdp->sd_lockstruct.ls_jid = jid;
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100487 clear_bit(SDF_NOJOURNALID, &sdp->sd_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100488 smp_mb__after_atomic();
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100489 wake_up_bit(&sdp->sd_flags, SDF_NOJOURNALID);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100490out:
491 spin_unlock(&sdp->sd_jindex_spin);
492 return rv ? rv : len;
493}
494
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000495#define GDLM_ATTR(_name,_mode,_show,_store) \
Steven Whitehouse48c2b612009-05-13 14:49:48 +0100496static struct gfs2_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000497
Steven Whitehoused7e623d2009-08-11 11:20:11 +0100498GDLM_ATTR(proto_name, 0444, proto_name_show, NULL);
499GDLM_ATTR(block, 0644, block_show, block_store);
Steven Whitehousefd95e812013-02-13 12:21:40 +0000500GDLM_ATTR(withdraw, 0644, wdack_show, wdack_store);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100501GDLM_ATTR(jid, 0644, jid_show, jid_store);
502GDLM_ATTR(first, 0644, lkfirst_show, lkfirst_store);
Steven Whitehoused7e623d2009-08-11 11:20:11 +0100503GDLM_ATTR(first_done, 0444, first_done_show, NULL);
504GDLM_ATTR(recover, 0600, NULL, recover_store);
505GDLM_ATTR(recover_done, 0444, recover_done_show, NULL);
506GDLM_ATTR(recover_status, 0444, recover_status_show, NULL);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000507
508static struct attribute *lock_module_attrs[] = {
509 &gdlm_attr_proto_name.attr,
510 &gdlm_attr_block.attr,
511 &gdlm_attr_withdraw.attr,
Steven Whitehousee1b28aa2009-05-26 15:41:27 +0100512 &gdlm_attr_jid.attr,
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000513 &gdlm_attr_first.attr,
514 &gdlm_attr_first_done.attr,
515 &gdlm_attr_recover.attr,
516 &gdlm_attr_recover_done.attr,
517 &gdlm_attr_recover_status.attr,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400518 NULL,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000519};
520
David Teiglandb3b94fa2006-01-16 16:50:04 +0000521/*
David Teiglandb3b94fa2006-01-16 16:50:04 +0000522 * get and set struct gfs2_tune fields
523 */
524
525static ssize_t quota_scale_show(struct gfs2_sbd *sdp, char *buf)
526{
David Teigland3204a6c2006-09-06 16:57:06 -0500527 return snprintf(buf, PAGE_SIZE, "%u %u\n",
528 sdp->sd_tune.gt_quota_scale_num,
529 sdp->sd_tune.gt_quota_scale_den);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000530}
531
532static ssize_t quota_scale_store(struct gfs2_sbd *sdp, const char *buf,
533 size_t len)
534{
535 struct gfs2_tune *gt = &sdp->sd_tune;
536 unsigned int x, y;
537
538 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100539 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000540
541 if (sscanf(buf, "%u %u", &x, &y) != 2 || !y)
542 return -EINVAL;
543
544 spin_lock(&gt->gt_spin);
545 gt->gt_quota_scale_num = x;
546 gt->gt_quota_scale_den = y;
547 spin_unlock(&gt->gt_spin);
548 return len;
549}
550
551static ssize_t tune_set(struct gfs2_sbd *sdp, unsigned int *field,
552 int check_zero, const char *buf, size_t len)
553{
554 struct gfs2_tune *gt = &sdp->sd_tune;
555 unsigned int x;
556
557 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100558 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000559
560 x = simple_strtoul(buf, NULL, 0);
561
562 if (check_zero && !x)
563 return -EINVAL;
564
565 spin_lock(&gt->gt_spin);
566 *field = x;
567 spin_unlock(&gt->gt_spin);
568 return len;
569}
570
David Teiglandb3b94fa2006-01-16 16:50:04 +0000571#define TUNE_ATTR_3(name, show, store) \
Steven Whitehouse48c2b612009-05-13 14:49:48 +0100572static struct gfs2_attr tune_attr_##name = __ATTR(name, 0644, show, store)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000573
574#define TUNE_ATTR_2(name, store) \
575static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf) \
576{ \
David Teigland3204a6c2006-09-06 16:57:06 -0500577 return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name); \
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578} \
579TUNE_ATTR_3(name, name##_show, store)
580
581#define TUNE_ATTR(name, check_zero) \
582static ssize_t name##_store(struct gfs2_sbd *sdp, const char *buf, size_t len)\
583{ \
584 return tune_set(sdp, &sdp->sd_tune.gt_##name, check_zero, buf, len); \
585} \
586TUNE_ATTR_2(name, name##_store)
587
David Teiglandb3b94fa2006-01-16 16:50:04 +0000588TUNE_ATTR(quota_warn_period, 0);
589TUNE_ATTR(quota_quantum, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000590TUNE_ATTR(max_readahead, 0);
591TUNE_ATTR(complain_secs, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000592TUNE_ATTR(statfs_slow, 0);
593TUNE_ATTR(new_files_jdata, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000594TUNE_ATTR(statfs_quantum, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000595TUNE_ATTR_3(quota_scale, quota_scale_show, quota_scale_store);
596
597static struct attribute *tune_attrs[] = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000598 &tune_attr_quota_warn_period.attr,
599 &tune_attr_quota_quantum.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000600 &tune_attr_max_readahead.attr,
601 &tune_attr_complain_secs.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000602 &tune_attr_statfs_slow.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000603 &tune_attr_statfs_quantum.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000604 &tune_attr_quota_scale.attr,
605 &tune_attr_new_files_jdata.attr,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400606 NULL,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000607};
608
David Teiglandb3b94fa2006-01-16 16:50:04 +0000609static struct attribute_group tune_group = {
610 .name = "tune",
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400611 .attrs = tune_attrs,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612};
613
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000614static struct attribute_group lock_module_group = {
615 .name = "lock_module",
616 .attrs = lock_module_attrs,
617};
618
David Teiglandb3b94fa2006-01-16 16:50:04 +0000619int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
620{
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100621 struct super_block *sb = sdp->sd_vfs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000622 int error;
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100623 char ro[20];
624 char spectator[20];
625 char *envp[] = { ro, spectator, NULL };
Bob Peterson0d515212012-06-13 10:27:41 -0400626 int sysfs_frees_sdp = 0;
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100627
628 sprintf(ro, "RDONLY=%d", (sb->s_flags & MS_RDONLY) ? 1 : 0);
629 sprintf(spectator, "SPECTATOR=%d", sdp->sd_args.ar_spectator ? 1 : 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000630
Greg Kroah-Hartman9bec1012007-10-29 20:13:17 +0100631 sdp->sd_kobj.kset = gfs2_kset;
Greg Kroah-Hartman901195e2007-12-17 15:54:39 -0400632 error = kobject_init_and_add(&sdp->sd_kobj, &gfs2_ktype, NULL,
633 "%s", sdp->sd_table_name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000634 if (error)
Bob Peterson0d515212012-06-13 10:27:41 -0400635 goto fail_reg;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000636
Bob Peterson0d515212012-06-13 10:27:41 -0400637 sysfs_frees_sdp = 1; /* Freeing sdp is now done by sysfs calling
638 function gfs2_sbd_release. */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000639 error = sysfs_create_group(&sdp->sd_kobj, &tune_group);
640 if (error)
Steven Whitehousef6eb5342009-05-26 15:50:25 +0100641 goto fail_reg;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000642
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000643 error = sysfs_create_group(&sdp->sd_kobj, &lock_module_group);
644 if (error)
645 goto fail_tune;
646
Steven Whitehouse31e54b02009-08-13 12:18:08 +0100647 error = sysfs_create_link(&sdp->sd_kobj,
648 &disk_to_dev(sb->s_bdev->bd_disk)->kobj,
649 "device");
650 if (error)
651 goto fail_lock_module;
652
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100653 kobject_uevent_env(&sdp->sd_kobj, KOBJ_ADD, envp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000654 return 0;
655
Steven Whitehouse31e54b02009-08-13 12:18:08 +0100656fail_lock_module:
657 sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000658fail_tune:
659 sysfs_remove_group(&sdp->sd_kobj, &tune_group);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400660fail_reg:
Bob Peterson0d515212012-06-13 10:27:41 -0400661 free_percpu(sdp->sd_lkstats);
David Teigland65952fb2006-09-15 13:09:11 -0500662 fs_err(sdp, "error %d adding sysfs files", error);
Bob Peterson0d515212012-06-13 10:27:41 -0400663 if (sysfs_frees_sdp)
664 kobject_put(&sdp->sd_kobj);
665 else
666 kfree(sdp);
667 sb->s_fs_info = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000668 return error;
669}
670
671void gfs2_sys_fs_del(struct gfs2_sbd *sdp)
672{
Steven Whitehouse31e54b02009-08-13 12:18:08 +0100673 sysfs_remove_link(&sdp->sd_kobj, "device");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000674 sysfs_remove_group(&sdp->sd_kobj, &tune_group);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000675 sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800676 kobject_put(&sdp->sd_kobj);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000677}
678
Steven Whitehousefdd10622008-11-26 10:26:38 +0000679static int gfs2_uevent(struct kset *kset, struct kobject *kobj,
680 struct kobj_uevent_env *env)
681{
682 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
Steven Whitehouse32e471e2011-05-10 15:01:59 +0100683 struct super_block *s = sdp->sd_vfs;
684 const u8 *uuid = s->s_uuid;
Steven Whitehouse02e3cc72009-02-10 13:48:30 +0000685
Steven Whitehousefdd10622008-11-26 10:26:38 +0000686 add_uevent_var(env, "LOCKTABLE=%s", sdp->sd_table_name);
687 add_uevent_var(env, "LOCKPROTO=%s", sdp->sd_proto_name);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100688 if (!test_bit(SDF_NOJOURNALID, &sdp->sd_flags))
Steven Whitehousefeb47ca92010-09-29 15:04:18 +0100689 add_uevent_var(env, "JOURNALID=%d", sdp->sd_lockstruct.ls_jid);
Joe Perchesf0b34ae2009-12-14 18:01:13 -0800690 if (gfs2_uuid_valid(uuid))
691 add_uevent_var(env, "UUID=%pUB", uuid);
Steven Whitehousefdd10622008-11-26 10:26:38 +0000692 return 0;
693}
694
Emese Revfy9cd43612009-12-31 14:52:51 +0100695static const struct kset_uevent_ops gfs2_uevent_ops = {
Steven Whitehousefdd10622008-11-26 10:26:38 +0000696 .uevent = gfs2_uevent,
697};
698
David Teiglandb3b94fa2006-01-16 16:50:04 +0000699int gfs2_sys_init(void)
700{
Steven Whitehousefdd10622008-11-26 10:26:38 +0000701 gfs2_kset = kset_create_and_add("gfs2", &gfs2_uevent_ops, fs_kobj);
Greg Kroah-Hartman9bec1012007-10-29 20:13:17 +0100702 if (!gfs2_kset)
703 return -ENOMEM;
704 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000705}
706
707void gfs2_sys_uninit(void)
708{
Greg Kroah-Hartman9bec1012007-10-29 20:13:17 +0100709 kset_unregister(gfs2_kset);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000710}
711