blob: 256354cba4dd8aa6d3e1fb46d4c1cf51fa3f7fc5 [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
143 gfs2_lm_withdraw(sdp,
144 "GFS2: fsid=%s: withdrawing from cluster at user's request\n",
145 sdp->sd_fsname);
146 return len;
147}
148
149static ssize_t statfs_sync_store(struct gfs2_sbd *sdp, const char *buf,
150 size_t len)
151{
152 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100153 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000154
155 if (simple_strtol(buf, NULL, 0) != 1)
156 return -EINVAL;
157
Steven Whitehouse8c42d632009-09-11 14:36:44 +0100158 gfs2_statfs_sync(sdp->sd_vfs, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000159 return len;
160}
161
David Teiglandb3b94fa2006-01-16 16:50:04 +0000162static ssize_t quota_sync_store(struct gfs2_sbd *sdp, const char *buf,
163 size_t len)
164{
165 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100166 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000167
168 if (simple_strtol(buf, NULL, 0) != 1)
169 return -EINVAL;
170
Jan Karaceed1722012-07-03 16:45:28 +0200171 gfs2_quota_sync(sdp->sd_vfs, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172 return len;
173}
174
175static ssize_t quota_refresh_user_store(struct gfs2_sbd *sdp, const char *buf,
176 size_t len)
177{
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800178 struct kqid qid;
Steven Whitehouseea762332009-09-15 16:20:30 +0100179 int error;
Steven Whitehousecd915492006-09-04 12:49:07 -0400180 u32 id;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000181
182 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100183 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000184
185 id = simple_strtoul(buf, NULL, 0);
186
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800187 qid = make_kqid(current_user_ns(), USRQUOTA, id);
188 if (!qid_valid(qid))
189 return -EINVAL;
190
191 error = gfs2_quota_refresh(sdp, qid);
Steven Whitehouseea762332009-09-15 16:20:30 +0100192 return error ? error : len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000193}
194
195static ssize_t quota_refresh_group_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
205 id = simple_strtoul(buf, NULL, 0);
206
Eric W. Biedermaned87dab2013-01-31 19:42:40 -0800207 qid = make_kqid(current_user_ns(), GRPQUOTA, id);
208 if (!qid_valid(qid))
209 return -EINVAL;
210
211 error = gfs2_quota_refresh(sdp, qid);
Steven Whitehouseea762332009-09-15 16:20:30 +0100212 return error ? error : len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213}
214
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000215static ssize_t demote_rq_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
216{
217 struct gfs2_glock *gl;
218 const struct gfs2_glock_operations *glops;
219 unsigned int glmode;
220 unsigned int gltype;
221 unsigned long long glnum;
222 char mode[16];
223 int rv;
224
225 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100226 return -EPERM;
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000227
228 rv = sscanf(buf, "%u:%llu %15s", &gltype, &glnum,
229 mode);
230 if (rv != 3)
231 return -EINVAL;
232
233 if (strcmp(mode, "EX") == 0)
234 glmode = LM_ST_UNLOCKED;
235 else if ((strcmp(mode, "CW") == 0) || (strcmp(mode, "DF") == 0))
236 glmode = LM_ST_DEFERRED;
237 else if ((strcmp(mode, "PR") == 0) || (strcmp(mode, "SH") == 0))
238 glmode = LM_ST_SHARED;
239 else
240 return -EINVAL;
241
242 if (gltype > LM_TYPE_JOURNAL)
243 return -EINVAL;
Steven Whitehouse13466982010-10-06 09:58:44 +0100244 if (gltype == LM_TYPE_NONDISK && glnum == GFS2_TRANS_LOCK)
245 glops = &gfs2_trans_glops;
246 else
247 glops = gfs2_glops_list[gltype];
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000248 if (glops == NULL)
249 return -EINVAL;
Steven Whitehouse6a99be52010-05-14 14:05:51 +0100250 if (!test_and_set_bit(SDF_DEMOTE, &sdp->sd_flags))
Steven Whitehouse913a71d2010-05-06 11:03:29 +0100251 fs_info(sdp, "demote interface used\n");
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000252 rv = gfs2_glock_get(sdp, glnum, glops, 0, &gl);
253 if (rv)
254 return rv;
255 gfs2_glock_cb(gl, glmode);
256 gfs2_glock_put(gl);
257 return len;
258}
259
David Teiglandb3b94fa2006-01-16 16:50:04 +0000260
261#define GFS2_ATTR(name, mode, show, store) \
262static struct gfs2_attr gfs2_attr_##name = __ATTR(name, mode, show, store)
263
264GFS2_ATTR(id, 0444, id_show, NULL);
265GFS2_ATTR(fsname, 0444, fsname_show, NULL);
Steven Whitehouse02e3cc72009-02-10 13:48:30 +0000266GFS2_ATTR(uuid, 0444, uuid_show, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000267GFS2_ATTR(freeze, 0644, freeze_show, freeze_store);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000268GFS2_ATTR(withdraw, 0644, withdraw_show, withdraw_store);
269GFS2_ATTR(statfs_sync, 0200, NULL, statfs_sync_store);
270GFS2_ATTR(quota_sync, 0200, NULL, quota_sync_store);
271GFS2_ATTR(quota_refresh_user, 0200, NULL, quota_refresh_user_store);
272GFS2_ATTR(quota_refresh_group, 0200, NULL, quota_refresh_group_store);
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000273GFS2_ATTR(demote_rq, 0200, NULL, demote_rq_store);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000274
275static struct attribute *gfs2_attrs[] = {
276 &gfs2_attr_id.attr,
277 &gfs2_attr_fsname.attr,
Steven Whitehouse02e3cc72009-02-10 13:48:30 +0000278 &gfs2_attr_uuid.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000279 &gfs2_attr_freeze.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000280 &gfs2_attr_withdraw.attr,
281 &gfs2_attr_statfs_sync.attr,
282 &gfs2_attr_quota_sync.attr,
283 &gfs2_attr_quota_refresh_user.attr,
284 &gfs2_attr_quota_refresh_group.attr,
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000285 &gfs2_attr_demote_rq.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000286 NULL,
287};
288
Bob Peterson0d515212012-06-13 10:27:41 -0400289static void gfs2_sbd_release(struct kobject *kobj)
290{
291 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
292
293 kfree(sdp);
294}
295
David Teiglandb3b94fa2006-01-16 16:50:04 +0000296static struct kobj_type gfs2_ktype = {
Bob Peterson0d515212012-06-13 10:27:41 -0400297 .release = gfs2_sbd_release,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000298 .default_attrs = gfs2_attrs,
299 .sysfs_ops = &gfs2_attr_ops,
300};
301
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000302
303/*
304 * lock_module. Originally from lock_dlm
305 */
306
307static ssize_t proto_name_show(struct gfs2_sbd *sdp, char *buf)
308{
309 const struct lm_lockops *ops = sdp->sd_lockstruct.ls_ops;
310 return sprintf(buf, "%s\n", ops->lm_proto_name);
311}
312
313static ssize_t block_show(struct gfs2_sbd *sdp, char *buf)
314{
315 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
316 ssize_t ret;
317 int val = 0;
318
David Teiglande0c2a9a2012-01-09 17:18:05 -0500319 if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000320 val = 1;
321 ret = sprintf(buf, "%d\n", val);
322 return ret;
323}
324
325static ssize_t block_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
326{
327 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
328 ssize_t ret = len;
329 int val;
330
331 val = simple_strtol(buf, NULL, 0);
332
333 if (val == 1)
David Teiglande0c2a9a2012-01-09 17:18:05 -0500334 set_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000335 else if (val == 0) {
David Teiglande0c2a9a2012-01-09 17:18:05 -0500336 clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000337 smp_mb__after_clear_bit();
338 gfs2_glock_thaw(sdp);
339 } else {
340 ret = -EINVAL;
341 }
342 return ret;
343}
344
Steven Whitehousefd95e812013-02-13 12:21:40 +0000345static ssize_t wdack_show(struct gfs2_sbd *sdp, char *buf)
346{
347 int val = completion_done(&sdp->sd_wdack) ? 1 : 0;
348
349 return sprintf(buf, "%d\n", val);
350}
351
352static ssize_t wdack_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
353{
354 ssize_t ret = len;
355 int val;
356
357 val = simple_strtol(buf, NULL, 0);
358
359 if ((val == 1) &&
360 !strcmp(sdp->sd_lockstruct.ls_ops->lm_proto_name, "lock_dlm"))
361 complete(&sdp->sd_wdack);
362 else
363 ret = -EINVAL;
364 return ret;
365}
366
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000367static ssize_t lkfirst_show(struct gfs2_sbd *sdp, char *buf)
368{
369 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
370 return sprintf(buf, "%d\n", ls->ls_first);
371}
372
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100373static ssize_t lkfirst_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
374{
375 unsigned first;
376 int rv;
377
378 rv = sscanf(buf, "%u", &first);
379 if (rv != 1 || first > 1)
380 return -EINVAL;
Steven Whitehouse3942ae52011-07-11 08:53:30 +0100381 rv = wait_for_completion_killable(&sdp->sd_locking_init);
382 if (rv)
383 return rv;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100384 spin_lock(&sdp->sd_jindex_spin);
385 rv = -EBUSY;
386 if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
387 goto out;
388 rv = -EINVAL;
389 if (sdp->sd_args.ar_spectator)
390 goto out;
391 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
392 goto out;
David Teiglande0c2a9a2012-01-09 17:18:05 -0500393 sdp->sd_lockstruct.ls_first = first;
394 rv = 0;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100395out:
396 spin_unlock(&sdp->sd_jindex_spin);
397 return rv ? rv : len;
398}
399
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000400static ssize_t first_done_show(struct gfs2_sbd *sdp, char *buf)
401{
402 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
David Teiglande0c2a9a2012-01-09 17:18:05 -0500403 return sprintf(buf, "%d\n", !!test_bit(DFL_FIRST_MOUNT_DONE, &ls->ls_recover_flags));
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000404}
405
David Teiglande0c2a9a2012-01-09 17:18:05 -0500406int gfs2_recover_set(struct gfs2_sbd *sdp, unsigned jid)
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000407{
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000408 struct gfs2_jdesc *jd;
Steven Whitehousefe64d512009-05-19 10:01:18 +0100409 int rv;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000410
411 spin_lock(&sdp->sd_jindex_spin);
Steven Whitehousefe64d512009-05-19 10:01:18 +0100412 rv = -EBUSY;
413 if (sdp->sd_jdesc->jd_jid == jid)
414 goto out;
415 rv = -ENOENT;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000416 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
417 if (jd->jd_jid != jid)
418 continue;
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200419 rv = gfs2_recover_journal(jd, false);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000420 break;
421 }
Steven Whitehousefe64d512009-05-19 10:01:18 +0100422out:
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000423 spin_unlock(&sdp->sd_jindex_spin);
David Teiglande0c2a9a2012-01-09 17:18:05 -0500424 return rv;
425}
426
427static ssize_t recover_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
428{
429 unsigned jid;
430 int rv;
431
432 rv = sscanf(buf, "%u", &jid);
433 if (rv != 1)
434 return -EINVAL;
435
David Teigland1a058f52012-05-01 15:50:48 -0500436 if (test_bit(SDF_NORECOVERY, &sdp->sd_flags)) {
437 rv = -ESHUTDOWN;
438 goto out;
439 }
David Teiglande0c2a9a2012-01-09 17:18:05 -0500440
David Teigland1a058f52012-05-01 15:50:48 -0500441 rv = gfs2_recover_set(sdp, jid);
442out:
Steven Whitehousefe64d512009-05-19 10:01:18 +0100443 return rv ? rv : len;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000444}
445
446static ssize_t recover_done_show(struct gfs2_sbd *sdp, char *buf)
447{
448 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
449 return sprintf(buf, "%d\n", ls->ls_recover_jid_done);
450}
451
452static ssize_t recover_status_show(struct gfs2_sbd *sdp, char *buf)
453{
454 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
455 return sprintf(buf, "%d\n", ls->ls_recover_jid_status);
456}
457
Steven Whitehousee1b28aa2009-05-26 15:41:27 +0100458static ssize_t jid_show(struct gfs2_sbd *sdp, char *buf)
459{
Steven Whitehousefeb47ca2010-09-29 15:04:18 +0100460 return sprintf(buf, "%d\n", sdp->sd_lockstruct.ls_jid);
Steven Whitehousee1b28aa2009-05-26 15:41:27 +0100461}
462
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100463static ssize_t jid_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
464{
Steven Whitehousefeb47ca2010-09-29 15:04:18 +0100465 int jid;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100466 int rv;
467
Steven Whitehousefeb47ca2010-09-29 15:04:18 +0100468 rv = sscanf(buf, "%d", &jid);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100469 if (rv != 1)
470 return -EINVAL;
Steven Whitehouse3942ae52011-07-11 08:53:30 +0100471 rv = wait_for_completion_killable(&sdp->sd_locking_init);
472 if (rv)
473 return rv;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100474 spin_lock(&sdp->sd_jindex_spin);
475 rv = -EINVAL;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100476 if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
477 goto out;
478 rv = -EBUSY;
Steven Whitehousefeb47ca2010-09-29 15:04:18 +0100479 if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100480 goto out;
Steven Whitehousefeb47ca2010-09-29 15:04:18 +0100481 rv = 0;
482 if (sdp->sd_args.ar_spectator && jid > 0)
483 rv = jid = -EINVAL;
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100484 sdp->sd_lockstruct.ls_jid = jid;
Steven Whitehousefeb47ca2010-09-29 15:04:18 +0100485 clear_bit(SDF_NOJOURNALID, &sdp->sd_flags);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100486 smp_mb__after_clear_bit();
487 wake_up_bit(&sdp->sd_flags, SDF_NOJOURNALID);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100488out:
489 spin_unlock(&sdp->sd_jindex_spin);
490 return rv ? rv : len;
491}
492
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000493#define GDLM_ATTR(_name,_mode,_show,_store) \
Steven Whitehouse48c2b612009-05-13 14:49:48 +0100494static struct gfs2_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000495
Steven Whitehoused7e623d2009-08-11 11:20:11 +0100496GDLM_ATTR(proto_name, 0444, proto_name_show, NULL);
497GDLM_ATTR(block, 0644, block_show, block_store);
Steven Whitehousefd95e812013-02-13 12:21:40 +0000498GDLM_ATTR(withdraw, 0644, wdack_show, wdack_store);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100499GDLM_ATTR(jid, 0644, jid_show, jid_store);
500GDLM_ATTR(first, 0644, lkfirst_show, lkfirst_store);
Steven Whitehoused7e623d2009-08-11 11:20:11 +0100501GDLM_ATTR(first_done, 0444, first_done_show, NULL);
502GDLM_ATTR(recover, 0600, NULL, recover_store);
503GDLM_ATTR(recover_done, 0444, recover_done_show, NULL);
504GDLM_ATTR(recover_status, 0444, recover_status_show, NULL);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000505
506static struct attribute *lock_module_attrs[] = {
507 &gdlm_attr_proto_name.attr,
508 &gdlm_attr_block.attr,
509 &gdlm_attr_withdraw.attr,
Steven Whitehousee1b28aa2009-05-26 15:41:27 +0100510 &gdlm_attr_jid.attr,
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000511 &gdlm_attr_first.attr,
512 &gdlm_attr_first_done.attr,
513 &gdlm_attr_recover.attr,
514 &gdlm_attr_recover_done.attr,
515 &gdlm_attr_recover_status.attr,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400516 NULL,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000517};
518
David Teiglandb3b94fa2006-01-16 16:50:04 +0000519/*
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520 * get and set struct gfs2_tune fields
521 */
522
523static ssize_t quota_scale_show(struct gfs2_sbd *sdp, char *buf)
524{
David Teigland3204a6c2006-09-06 16:57:06 -0500525 return snprintf(buf, PAGE_SIZE, "%u %u\n",
526 sdp->sd_tune.gt_quota_scale_num,
527 sdp->sd_tune.gt_quota_scale_den);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000528}
529
530static ssize_t quota_scale_store(struct gfs2_sbd *sdp, const char *buf,
531 size_t len)
532{
533 struct gfs2_tune *gt = &sdp->sd_tune;
534 unsigned int x, y;
535
536 if (!capable(CAP_SYS_ADMIN))
Zhao Hongjiang41735812013-02-20 13:13:55 +1100537 return -EPERM;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000538
539 if (sscanf(buf, "%u %u", &x, &y) != 2 || !y)
540 return -EINVAL;
541
542 spin_lock(&gt->gt_spin);
543 gt->gt_quota_scale_num = x;
544 gt->gt_quota_scale_den = y;
545 spin_unlock(&gt->gt_spin);
546 return len;
547}
548
549static ssize_t tune_set(struct gfs2_sbd *sdp, unsigned int *field,
550 int check_zero, const char *buf, size_t len)
551{
552 struct gfs2_tune *gt = &sdp->sd_tune;
553 unsigned int x;
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 x = simple_strtoul(buf, NULL, 0);
559
560 if (check_zero && !x)
561 return -EINVAL;
562
563 spin_lock(&gt->gt_spin);
564 *field = x;
565 spin_unlock(&gt->gt_spin);
566 return len;
567}
568
David Teiglandb3b94fa2006-01-16 16:50:04 +0000569#define TUNE_ATTR_3(name, show, store) \
Steven Whitehouse48c2b612009-05-13 14:49:48 +0100570static struct gfs2_attr tune_attr_##name = __ATTR(name, 0644, show, store)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000571
572#define TUNE_ATTR_2(name, store) \
573static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf) \
574{ \
David Teigland3204a6c2006-09-06 16:57:06 -0500575 return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name); \
David Teiglandb3b94fa2006-01-16 16:50:04 +0000576} \
577TUNE_ATTR_3(name, name##_show, store)
578
579#define TUNE_ATTR(name, check_zero) \
580static ssize_t name##_store(struct gfs2_sbd *sdp, const char *buf, size_t len)\
581{ \
582 return tune_set(sdp, &sdp->sd_tune.gt_##name, check_zero, buf, len); \
583} \
584TUNE_ATTR_2(name, name##_store)
585
David Teiglandb3b94fa2006-01-16 16:50:04 +0000586TUNE_ATTR(quota_warn_period, 0);
587TUNE_ATTR(quota_quantum, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000588TUNE_ATTR(max_readahead, 0);
589TUNE_ATTR(complain_secs, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000590TUNE_ATTR(statfs_slow, 0);
591TUNE_ATTR(new_files_jdata, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000592TUNE_ATTR(statfs_quantum, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000593TUNE_ATTR_3(quota_scale, quota_scale_show, quota_scale_store);
594
595static struct attribute *tune_attrs[] = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000596 &tune_attr_quota_warn_period.attr,
597 &tune_attr_quota_quantum.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000598 &tune_attr_max_readahead.attr,
599 &tune_attr_complain_secs.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000600 &tune_attr_statfs_slow.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000601 &tune_attr_statfs_quantum.attr,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000602 &tune_attr_quota_scale.attr,
603 &tune_attr_new_files_jdata.attr,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400604 NULL,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000605};
606
David Teiglandb3b94fa2006-01-16 16:50:04 +0000607static struct attribute_group tune_group = {
608 .name = "tune",
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400609 .attrs = tune_attrs,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610};
611
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000612static struct attribute_group lock_module_group = {
613 .name = "lock_module",
614 .attrs = lock_module_attrs,
615};
616
David Teiglandb3b94fa2006-01-16 16:50:04 +0000617int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
618{
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100619 struct super_block *sb = sdp->sd_vfs;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000620 int error;
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100621 char ro[20];
622 char spectator[20];
623 char *envp[] = { ro, spectator, NULL };
Bob Peterson0d515212012-06-13 10:27:41 -0400624 int sysfs_frees_sdp = 0;
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100625
626 sprintf(ro, "RDONLY=%d", (sb->s_flags & MS_RDONLY) ? 1 : 0);
627 sprintf(spectator, "SPECTATOR=%d", sdp->sd_args.ar_spectator ? 1 : 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000628
Greg Kroah-Hartman9bec1012007-10-29 20:13:17 +0100629 sdp->sd_kobj.kset = gfs2_kset;
Greg Kroah-Hartman901195e2007-12-17 15:54:39 -0400630 error = kobject_init_and_add(&sdp->sd_kobj, &gfs2_ktype, NULL,
631 "%s", sdp->sd_table_name);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000632 if (error)
Bob Peterson0d515212012-06-13 10:27:41 -0400633 goto fail_reg;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000634
Bob Peterson0d515212012-06-13 10:27:41 -0400635 sysfs_frees_sdp = 1; /* Freeing sdp is now done by sysfs calling
636 function gfs2_sbd_release. */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000637 error = sysfs_create_group(&sdp->sd_kobj, &tune_group);
638 if (error)
Steven Whitehousef6eb5342009-05-26 15:50:25 +0100639 goto fail_reg;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000640
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000641 error = sysfs_create_group(&sdp->sd_kobj, &lock_module_group);
642 if (error)
643 goto fail_tune;
644
Steven Whitehouse31e54b02009-08-13 12:18:08 +0100645 error = sysfs_create_link(&sdp->sd_kobj,
646 &disk_to_dev(sb->s_bdev->bd_disk)->kobj,
647 "device");
648 if (error)
649 goto fail_lock_module;
650
Steven Whitehouse440d6da2009-07-31 12:16:25 +0100651 kobject_uevent_env(&sdp->sd_kobj, KOBJ_ADD, envp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000652 return 0;
653
Steven Whitehouse31e54b02009-08-13 12:18:08 +0100654fail_lock_module:
655 sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000656fail_tune:
657 sysfs_remove_group(&sdp->sd_kobj, &tune_group);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400658fail_reg:
Bob Peterson0d515212012-06-13 10:27:41 -0400659 free_percpu(sdp->sd_lkstats);
David Teigland65952fb2006-09-15 13:09:11 -0500660 fs_err(sdp, "error %d adding sysfs files", error);
Bob Peterson0d515212012-06-13 10:27:41 -0400661 if (sysfs_frees_sdp)
662 kobject_put(&sdp->sd_kobj);
663 else
664 kfree(sdp);
665 sb->s_fs_info = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000666 return error;
667}
668
669void gfs2_sys_fs_del(struct gfs2_sbd *sdp)
670{
Steven Whitehouse31e54b02009-08-13 12:18:08 +0100671 sysfs_remove_link(&sdp->sd_kobj, "device");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000672 sysfs_remove_group(&sdp->sd_kobj, &tune_group);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000673 sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800674 kobject_put(&sdp->sd_kobj);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000675}
676
Steven Whitehousefdd10622008-11-26 10:26:38 +0000677static int gfs2_uevent(struct kset *kset, struct kobject *kobj,
678 struct kobj_uevent_env *env)
679{
680 struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
Steven Whitehouse32e471e2011-05-10 15:01:59 +0100681 struct super_block *s = sdp->sd_vfs;
682 const u8 *uuid = s->s_uuid;
Steven Whitehouse02e3cc72009-02-10 13:48:30 +0000683
Steven Whitehousefdd10622008-11-26 10:26:38 +0000684 add_uevent_var(env, "LOCKTABLE=%s", sdp->sd_table_name);
685 add_uevent_var(env, "LOCKPROTO=%s", sdp->sd_proto_name);
Steven Whitehouseba6e9362010-06-14 10:01:30 +0100686 if (!test_bit(SDF_NOJOURNALID, &sdp->sd_flags))
Steven Whitehousefeb47ca2010-09-29 15:04:18 +0100687 add_uevent_var(env, "JOURNALID=%d", sdp->sd_lockstruct.ls_jid);
Joe Perchesf0b34ae2009-12-14 18:01:13 -0800688 if (gfs2_uuid_valid(uuid))
689 add_uevent_var(env, "UUID=%pUB", uuid);
Steven Whitehousefdd10622008-11-26 10:26:38 +0000690 return 0;
691}
692
Emese Revfy9cd43612009-12-31 14:52:51 +0100693static const struct kset_uevent_ops gfs2_uevent_ops = {
Steven Whitehousefdd10622008-11-26 10:26:38 +0000694 .uevent = gfs2_uevent,
695};
696
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697int gfs2_sys_init(void)
698{
Steven Whitehousefdd10622008-11-26 10:26:38 +0000699 gfs2_kset = kset_create_and_add("gfs2", &gfs2_uevent_ops, fs_kobj);
Greg Kroah-Hartman9bec1012007-10-29 20:13:17 +0100700 if (!gfs2_kset)
701 return -ENOMEM;
702 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000703}
704
705void gfs2_sys_uninit(void)
706{
Greg Kroah-Hartman9bec1012007-10-29 20:13:17 +0100707 kset_unregister(gfs2_kset);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000708}
709