Linux Audio

Check our new training course

Loading...
v3.15
 
  1/*
  2 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
  3 * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
  4 *
  5 * This copyrighted material is made available to anyone wishing to use,
  6 * modify, copy, or redistribute it subject to the terms and conditions
  7 * of the GNU General Public License version 2.
  8 */
  9
 10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 11
 12#include <linux/sched.h>
 
 13#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>
 18#include <asm/uaccess.h>
 19#include <linux/gfs2_ondisk.h>
 20#include <linux/genhd.h>
 21
 22#include "gfs2.h"
 23#include "incore.h"
 24#include "sys.h"
 25#include "super.h"
 26#include "glock.h"
 27#include "quota.h"
 28#include "util.h"
 29#include "glops.h"
 30#include "recovery.h"
 31
 32struct 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
 54static const struct sysfs_ops gfs2_attr_ops = {
 55	.show  = gfs2_attr_show,
 56	.store = gfs2_attr_store,
 57};
 58
 59
 60static struct kset *gfs2_kset;
 61
 62static ssize_t id_show(struct gfs2_sbd *sdp, char *buf)
 63{
 64	return snprintf(buf, PAGE_SIZE, "%u:%u\n",
 65			MAJOR(sdp->sd_vfs->s_dev), MINOR(sdp->sd_vfs->s_dev));
 66}
 67
 68static ssize_t fsname_show(struct gfs2_sbd *sdp, char *buf)
 69{
 70	return snprintf(buf, PAGE_SIZE, "%s\n", sdp->sd_fsname);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 71}
 72
 73static 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{
 86	struct super_block *s = sdp->sd_vfs;
 87	const u8 *uuid = s->s_uuid;
 88	buf[0] = '\0';
 89	if (!gfs2_uuid_valid(uuid))
 90		return 0;
 91	return snprintf(buf, PAGE_SIZE, "%pUB\n", uuid);
 92}
 93
 94static ssize_t freeze_show(struct gfs2_sbd *sdp, char *buf)
 95{
 96	struct super_block *sb = sdp->sd_vfs;
 97	int frozen = (sb->s_writers.frozen == SB_UNFROZEN) ? 0 : 1;
 98
 99	return snprintf(buf, PAGE_SIZE, "%u\n", frozen);
100}
101
102static ssize_t freeze_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
103{
104	int error;
105	int n = simple_strtol(buf, NULL, 0);
 
 
 
106
107	if (!capable(CAP_SYS_ADMIN))
108		return -EPERM;
109
110	switch (n) {
111	case 0:
112		error = thaw_super(sdp->sd_vfs);
113		break;
114	case 1:
115		error = freeze_super(sdp->sd_vfs);
116		break;
117	default:
118		return -EINVAL;
119	}
120
121	if (error) {
122		fs_warn(sdp, "freeze %d error %d", n, error);
123		return error;
124	}
125
126	return len;
127}
128
129static ssize_t withdraw_show(struct gfs2_sbd *sdp, char *buf)
130{
131	unsigned int b = test_bit(SDF_SHUTDOWN, &sdp->sd_flags);
132	return snprintf(buf, PAGE_SIZE, "%u\n", b);
133}
134
135static ssize_t withdraw_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
136{
 
 
137	if (!capable(CAP_SYS_ADMIN))
138		return -EPERM;
139
140	if (simple_strtol(buf, NULL, 0) != 1)
 
 
 
 
141		return -EINVAL;
142
143	gfs2_lm_withdraw(sdp, "withdrawing from cluster at user's request\n");
 
144
145	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))
152		return -EPERM;
153
154	if (simple_strtol(buf, NULL, 0) != 1)
 
 
 
 
155		return -EINVAL;
156
157	gfs2_statfs_sync(sdp->sd_vfs, 0);
158	return len;
159}
160
161static ssize_t quota_sync_store(struct gfs2_sbd *sdp, const char *buf,
162				size_t len)
163{
 
 
164	if (!capable(CAP_SYS_ADMIN))
165		return -EPERM;
166
167	if (simple_strtol(buf, NULL, 0) != 1)
 
 
 
 
168		return -EINVAL;
169
170	gfs2_quota_sync(sdp->sd_vfs, 0);
171	return len;
172}
173
174static ssize_t quota_refresh_user_store(struct gfs2_sbd *sdp, const char *buf,
175					size_t len)
176{
177	struct kqid qid;
178	int error;
179	u32 id;
180
181	if (!capable(CAP_SYS_ADMIN))
182		return -EPERM;
183
184	id = simple_strtoul(buf, NULL, 0);
 
 
185
186	qid = make_kqid(current_user_ns(), USRQUOTA, id);
187	if (!qid_valid(qid))
188		return -EINVAL;
189
190	error = gfs2_quota_refresh(sdp, qid);
191	return error ? error : len;
192}
193
194static ssize_t quota_refresh_group_store(struct gfs2_sbd *sdp, const char *buf,
195					 size_t len)
196{
197	struct kqid qid;
198	int error;
199	u32 id;
200
201	if (!capable(CAP_SYS_ADMIN))
202		return -EPERM;
203
204	id = simple_strtoul(buf, NULL, 0);
 
 
205
206	qid = make_kqid(current_user_ns(), GRPQUOTA, id);
207	if (!qid_valid(qid))
208		return -EINVAL;
209
210	error = gfs2_quota_refresh(sdp, qid);
211	return error ? error : len;
212}
213
214static 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))
225		return -EPERM;
226
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;
243	if (gltype == LM_TYPE_NONDISK && glnum == GFS2_TRANS_LOCK)
244		glops = &gfs2_trans_glops;
245	else
246		glops = gfs2_glops_list[gltype];
247	if (glops == NULL)
248		return -EINVAL;
249	if (!test_and_set_bit(SDF_DEMOTE, &sdp->sd_flags))
250		fs_info(sdp, "demote interface used\n");
251	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
259
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);
265GFS2_ATTR(uuid,                0444, uuid_show,     NULL);
266GFS2_ATTR(freeze,              0644, freeze_show,   freeze_store);
267GFS2_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);
272GFS2_ATTR(demote_rq,           0200, NULL,	    demote_rq_store);
 
273
274static struct attribute *gfs2_attrs[] = {
275	&gfs2_attr_id.attr,
276	&gfs2_attr_fsname.attr,
277	&gfs2_attr_uuid.attr,
278	&gfs2_attr_freeze.attr,
279	&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,
284	&gfs2_attr_demote_rq.attr,
 
285	NULL,
286};
 
287
288static 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
295static struct kobj_type gfs2_ktype = {
296	.release = gfs2_sbd_release,
297	.default_attrs = gfs2_attrs,
298	.sysfs_ops     = &gfs2_attr_ops,
299};
300
301
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
318	if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))
319		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)
333		set_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
334	else if (val == 0) {
335		clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
336		smp_mb__after_clear_bit();
337		gfs2_glock_thaw(sdp);
338	} else {
339		ret = -EINVAL;
340	}
341	return ret;
342}
343
344static 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
366static 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
372static 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;
380	rv = wait_for_completion_killable(&sdp->sd_locking_init);
381	if (rv)
382		return rv;
383	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;
392	sdp->sd_lockstruct.ls_first = first;
393	rv = 0;
394out:
395        spin_unlock(&sdp->sd_jindex_spin);
396        return rv ? rv : len;
397}
398
399static ssize_t first_done_show(struct gfs2_sbd *sdp, char *buf)
400{
401	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
402	return sprintf(buf, "%d\n", !!test_bit(DFL_FIRST_MOUNT_DONE, &ls->ls_recover_flags));
403}
404
405int gfs2_recover_set(struct gfs2_sbd *sdp, unsigned jid)
406{
407	struct gfs2_jdesc *jd;
408	int rv;
409
 
 
 
410	spin_lock(&sdp->sd_jindex_spin);
411	rv = -EBUSY;
412	if (sdp->sd_jdesc->jd_jid == jid)
 
 
 
 
 
 
 
 
 
413		goto out;
414	rv = -ENOENT;
415	list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
416		if (jd->jd_jid != jid)
417			continue;
418		rv = gfs2_recover_journal(jd, false);
419		break;
420	}
421out:
422	spin_unlock(&sdp->sd_jindex_spin);
423	return rv;
424}
425
426static ssize_t recover_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
427{
428	unsigned jid;
429	int rv;
430
431	rv = sscanf(buf, "%u", &jid);
432	if (rv != 1)
433		return -EINVAL;
434
435	if (test_bit(SDF_NORECOVERY, &sdp->sd_flags)) {
436		rv = -ESHUTDOWN;
437		goto out;
438	}
439
440	rv = gfs2_recover_set(sdp, jid);
441out:
442	return rv ? rv : len;
443}
444
445static ssize_t recover_done_show(struct gfs2_sbd *sdp, char *buf)
446{
447	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
448	return sprintf(buf, "%d\n", ls->ls_recover_jid_done);
449}
450
451static ssize_t recover_status_show(struct gfs2_sbd *sdp, char *buf)
452{
453	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
454	return sprintf(buf, "%d\n", ls->ls_recover_jid_status);
455}
456
457static ssize_t jid_show(struct gfs2_sbd *sdp, char *buf)
458{
459	return sprintf(buf, "%d\n", sdp->sd_lockstruct.ls_jid);
460}
461
462static ssize_t jid_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
463{
464        int jid;
465	int rv;
466
467	rv = sscanf(buf, "%d", &jid);
468	if (rv != 1)
469		return -EINVAL;
470	rv = wait_for_completion_killable(&sdp->sd_locking_init);
471	if (rv)
472		return rv;
473	spin_lock(&sdp->sd_jindex_spin);
474	rv = -EINVAL;
475	if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
476		goto out;
477	rv = -EBUSY;
478	if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
479		goto out;
480	rv = 0;
481	if (sdp->sd_args.ar_spectator && jid > 0)
482		rv = jid = -EINVAL;
483	sdp->sd_lockstruct.ls_jid = jid;
484	clear_bit(SDF_NOJOURNALID, &sdp->sd_flags);
485	smp_mb__after_clear_bit();
486	wake_up_bit(&sdp->sd_flags, SDF_NOJOURNALID);
487out:
488	spin_unlock(&sdp->sd_jindex_spin);
489	return rv ? rv : len;
490}
491
492#define GDLM_ATTR(_name,_mode,_show,_store) \
493static struct gfs2_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
494
495GDLM_ATTR(proto_name,		0444, proto_name_show,		NULL);
496GDLM_ATTR(block,		0644, block_show,		block_store);
497GDLM_ATTR(withdraw,		0644, wdack_show,		wdack_store);
498GDLM_ATTR(jid,			0644, jid_show,			jid_store);
499GDLM_ATTR(first,		0644, lkfirst_show,		lkfirst_store);
500GDLM_ATTR(first_done,		0444, first_done_show,		NULL);
501GDLM_ATTR(recover,		0600, NULL,			recover_store);
502GDLM_ATTR(recover_done,		0444, recover_done_show,	NULL);
503GDLM_ATTR(recover_status,	0444, recover_status_show,	NULL);
504
505static struct attribute *lock_module_attrs[] = {
506	&gdlm_attr_proto_name.attr,
507	&gdlm_attr_block.attr,
508	&gdlm_attr_withdraw.attr,
509	&gdlm_attr_jid.attr,
510	&gdlm_attr_first.attr,
511	&gdlm_attr_first_done.attr,
512	&gdlm_attr_recover.attr,
513	&gdlm_attr_recover_done.attr,
514	&gdlm_attr_recover_status.attr,
515	NULL,
516};
517
518/*
519 * get and set struct gfs2_tune fields
520 */
521
522static ssize_t quota_scale_show(struct gfs2_sbd *sdp, char *buf)
523{
524	return snprintf(buf, PAGE_SIZE, "%u %u\n",
525			sdp->sd_tune.gt_quota_scale_num,
526			sdp->sd_tune.gt_quota_scale_den);
527}
528
529static ssize_t quota_scale_store(struct gfs2_sbd *sdp, const char *buf,
530				 size_t len)
531{
532	struct gfs2_tune *gt = &sdp->sd_tune;
533	unsigned int x, y;
534
535	if (!capable(CAP_SYS_ADMIN))
536		return -EPERM;
537
538	if (sscanf(buf, "%u %u", &x, &y) != 2 || !y)
539		return -EINVAL;
540
541	spin_lock(&gt->gt_spin);
542	gt->gt_quota_scale_num = x;
543	gt->gt_quota_scale_den = y;
544	spin_unlock(&gt->gt_spin);
545	return len;
546}
547
548static ssize_t tune_set(struct gfs2_sbd *sdp, unsigned int *field,
549			int check_zero, const char *buf, size_t len)
550{
551	struct gfs2_tune *gt = &sdp->sd_tune;
552	unsigned int x;
 
553
554	if (!capable(CAP_SYS_ADMIN))
555		return -EPERM;
556
557	x = simple_strtoul(buf, NULL, 0);
 
 
558
559	if (check_zero && !x)
560		return -EINVAL;
561
562	spin_lock(&gt->gt_spin);
563	*field = x;
564	spin_unlock(&gt->gt_spin);
565	return len;
566}
567
568#define TUNE_ATTR_3(name, show, store)                                        \
569static struct gfs2_attr tune_attr_##name = __ATTR(name, 0644, show, store)
570
571#define TUNE_ATTR_2(name, store)                                              \
572static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf)                   \
573{                                                                             \
574	return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name);      \
575}                                                                             \
576TUNE_ATTR_3(name, name##_show, store)
577
578#define TUNE_ATTR(name, check_zero)                                           \
579static ssize_t name##_store(struct gfs2_sbd *sdp, const char *buf, size_t len)\
580{                                                                             \
581	return tune_set(sdp, &sdp->sd_tune.gt_##name, check_zero, buf, len);  \
582}                                                                             \
583TUNE_ATTR_2(name, name##_store)
584
585TUNE_ATTR(quota_warn_period, 0);
586TUNE_ATTR(quota_quantum, 0);
587TUNE_ATTR(max_readahead, 0);
588TUNE_ATTR(complain_secs, 0);
589TUNE_ATTR(statfs_slow, 0);
590TUNE_ATTR(new_files_jdata, 0);
591TUNE_ATTR(statfs_quantum, 1);
592TUNE_ATTR_3(quota_scale, quota_scale_show, quota_scale_store);
593
594static struct attribute *tune_attrs[] = {
595	&tune_attr_quota_warn_period.attr,
596	&tune_attr_quota_quantum.attr,
597	&tune_attr_max_readahead.attr,
598	&tune_attr_complain_secs.attr,
599	&tune_attr_statfs_slow.attr,
600	&tune_attr_statfs_quantum.attr,
601	&tune_attr_quota_scale.attr,
602	&tune_attr_new_files_jdata.attr,
603	NULL,
604};
605
606static struct attribute_group tune_group = {
607	.name = "tune",
608	.attrs = tune_attrs,
609};
610
611static struct attribute_group lock_module_group = {
612	.name = "lock_module",
613	.attrs = lock_module_attrs,
614};
615
616int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
617{
618	struct super_block *sb = sdp->sd_vfs;
619	int error;
620	char ro[20];
621	char spectator[20];
622	char *envp[] = { ro, spectator, NULL };
623	int sysfs_frees_sdp = 0;
624
625	sprintf(ro, "RDONLY=%d", (sb->s_flags & MS_RDONLY) ? 1 : 0);
626	sprintf(spectator, "SPECTATOR=%d", sdp->sd_args.ar_spectator ? 1 : 0);
627
 
628	sdp->sd_kobj.kset = gfs2_kset;
629	error = kobject_init_and_add(&sdp->sd_kobj, &gfs2_ktype, NULL,
630				     "%s", sdp->sd_table_name);
631	if (error)
632		goto fail_reg;
633
634	sysfs_frees_sdp = 1; /* Freeing sdp is now done by sysfs calling
635				function gfs2_sbd_release. */
636	error = sysfs_create_group(&sdp->sd_kobj, &tune_group);
637	if (error)
638		goto fail_reg;
639
640	error = sysfs_create_group(&sdp->sd_kobj, &lock_module_group);
641	if (error)
642		goto fail_tune;
643
644	error = sysfs_create_link(&sdp->sd_kobj,
645				  &disk_to_dev(sb->s_bdev->bd_disk)->kobj,
646				  "device");
647	if (error)
648		goto fail_lock_module;
649
650	kobject_uevent_env(&sdp->sd_kobj, KOBJ_ADD, envp);
651	return 0;
652
653fail_lock_module:
654	sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
655fail_tune:
656	sysfs_remove_group(&sdp->sd_kobj, &tune_group);
657fail_reg:
658	free_percpu(sdp->sd_lkstats);
659	fs_err(sdp, "error %d adding sysfs files", error);
660	if (sysfs_frees_sdp)
661		kobject_put(&sdp->sd_kobj);
662	else
663		kfree(sdp);
664	sb->s_fs_info = NULL;
665	return error;
666}
667
668void gfs2_sys_fs_del(struct gfs2_sbd *sdp)
669{
670	sysfs_remove_link(&sdp->sd_kobj, "device");
671	sysfs_remove_group(&sdp->sd_kobj, &tune_group);
672	sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
673	kobject_put(&sdp->sd_kobj);
 
674}
675
676static int gfs2_uevent(struct kset *kset, struct kobject *kobj,
677		       struct kobj_uevent_env *env)
678{
679	struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
680	struct super_block *s = sdp->sd_vfs;
681	const u8 *uuid = s->s_uuid;
682
683	add_uevent_var(env, "LOCKTABLE=%s", sdp->sd_table_name);
684	add_uevent_var(env, "LOCKPROTO=%s", sdp->sd_proto_name);
685	if (!test_bit(SDF_NOJOURNALID, &sdp->sd_flags))
686		add_uevent_var(env, "JOURNALID=%d", sdp->sd_lockstruct.ls_jid);
687	if (gfs2_uuid_valid(uuid))
688		add_uevent_var(env, "UUID=%pUB", uuid);
689	return 0;
690}
691
692static const struct kset_uevent_ops gfs2_uevent_ops = {
693	.uevent = gfs2_uevent,
694};
695
696int gfs2_sys_init(void)
697{
698	gfs2_kset = kset_create_and_add("gfs2", &gfs2_uevent_ops, fs_kobj);
699	if (!gfs2_kset)
700		return -ENOMEM;
701	return 0;
702}
703
704void gfs2_sys_uninit(void)
705{
706	kset_unregister(gfs2_kset);
707}
708
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
  4 * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
 
 
 
 
  5 */
  6
  7#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8
  9#include <linux/sched.h>
 10#include <linux/cred.h>
 11#include <linux/spinlock.h>
 12#include <linux/completion.h>
 13#include <linux/buffer_head.h>
 14#include <linux/module.h>
 15#include <linux/kobject.h>
 16#include <linux/uaccess.h>
 17#include <linux/gfs2_ondisk.h>
 18#include <linux/blkdev.h>
 19
 20#include "gfs2.h"
 21#include "incore.h"
 22#include "sys.h"
 23#include "super.h"
 24#include "glock.h"
 25#include "quota.h"
 26#include "util.h"
 27#include "glops.h"
 28#include "recovery.h"
 29
 30struct gfs2_attr {
 31	struct attribute attr;
 32	ssize_t (*show)(struct gfs2_sbd *, char *);
 33	ssize_t (*store)(struct gfs2_sbd *, const char *, size_t);
 34};
 35
 36static ssize_t gfs2_attr_show(struct kobject *kobj, struct attribute *attr,
 37			      char *buf)
 38{
 39	struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
 40	struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
 41	return a->show ? a->show(sdp, buf) : 0;
 42}
 43
 44static ssize_t gfs2_attr_store(struct kobject *kobj, struct attribute *attr,
 45			       const char *buf, size_t len)
 46{
 47	struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
 48	struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
 49	return a->store ? a->store(sdp, buf, len) : len;
 50}
 51
 52static const struct sysfs_ops gfs2_attr_ops = {
 53	.show  = gfs2_attr_show,
 54	.store = gfs2_attr_store,
 55};
 56
 57
 58static struct kset *gfs2_kset;
 59
 60static ssize_t id_show(struct gfs2_sbd *sdp, char *buf)
 61{
 62	return snprintf(buf, PAGE_SIZE, "%u:%u\n",
 63			MAJOR(sdp->sd_vfs->s_dev), MINOR(sdp->sd_vfs->s_dev));
 64}
 65
 66static ssize_t status_show(struct gfs2_sbd *sdp, char *buf)
 67{
 68	unsigned long f = sdp->sd_flags;
 69	ssize_t s;
 70
 71	s = snprintf(buf, PAGE_SIZE,
 72		     "Journal Checked:          %d\n"
 73		     "Journal Live:             %d\n"
 74		     "Journal ID:               %d\n"
 75		     "Spectator:                %d\n"
 76		     "Withdrawn:                %d\n"
 77		     "No barriers:              %d\n"
 78		     "No recovery:              %d\n"
 79		     "Demote:                   %d\n"
 80		     "No Journal ID:            %d\n"
 81		     "Mounted RO:               %d\n"
 82		     "RO Recovery:              %d\n"
 83		     "Skip DLM Unlock:          %d\n"
 84		     "Force AIL Flush:          %d\n"
 85		     "FS Freeze Initiator:      %d\n"
 86		     "FS Frozen:                %d\n"
 87		     "Withdrawing:              %d\n"
 88		     "Withdraw In Prog:         %d\n"
 89		     "Remote Withdraw:          %d\n"
 90		     "Withdraw Recovery:        %d\n"
 91		     "Killing:                  %d\n"
 92		     "sd_log_error:             %d\n"
 93		     "sd_log_flush_lock:        %d\n"
 94		     "sd_log_num_revoke:        %u\n"
 95		     "sd_log_in_flight:         %d\n"
 96		     "sd_log_blks_needed:       %d\n"
 97		     "sd_log_blks_free:         %d\n"
 98		     "sd_log_flush_head:        %d\n"
 99		     "sd_log_flush_tail:        %d\n"
100		     "sd_log_blks_reserved:     %d\n"
101		     "sd_log_revokes_available: %d\n"
102		     "sd_log_pinned:            %d\n"
103		     "sd_log_thresh1:           %d\n"
104		     "sd_log_thresh2:           %d\n",
105		     test_bit(SDF_JOURNAL_CHECKED, &f),
106		     test_bit(SDF_JOURNAL_LIVE, &f),
107		     (sdp->sd_jdesc ? sdp->sd_jdesc->jd_jid : 0),
108		     (sdp->sd_args.ar_spectator ? 1 : 0),
109		     test_bit(SDF_WITHDRAWN, &f),
110		     test_bit(SDF_NOBARRIERS, &f),
111		     test_bit(SDF_NORECOVERY, &f),
112		     test_bit(SDF_DEMOTE, &f),
113		     test_bit(SDF_NOJOURNALID, &f),
114		     (sb_rdonly(sdp->sd_vfs) ? 1 : 0),
115		     test_bit(SDF_RORECOVERY, &f),
116		     test_bit(SDF_SKIP_DLM_UNLOCK, &f),
117		     test_bit(SDF_FORCE_AIL_FLUSH, &f),
118		     test_bit(SDF_FREEZE_INITIATOR, &f),
119		     test_bit(SDF_FROZEN, &f),
120		     test_bit(SDF_WITHDRAWING, &f),
121		     test_bit(SDF_WITHDRAW_IN_PROG, &f),
122		     test_bit(SDF_REMOTE_WITHDRAW, &f),
123		     test_bit(SDF_WITHDRAW_RECOVERY, &f),
124		     test_bit(SDF_KILL, &f),
125		     sdp->sd_log_error,
126		     rwsem_is_locked(&sdp->sd_log_flush_lock),
127		     sdp->sd_log_num_revoke,
128		     atomic_read(&sdp->sd_log_in_flight),
129		     atomic_read(&sdp->sd_log_blks_needed),
130		     atomic_read(&sdp->sd_log_blks_free),
131		     sdp->sd_log_flush_head,
132		     sdp->sd_log_flush_tail,
133		     sdp->sd_log_blks_reserved,
134		     atomic_read(&sdp->sd_log_revokes_available),
135		     atomic_read(&sdp->sd_log_pinned),
136		     atomic_read(&sdp->sd_log_thresh1),
137		     atomic_read(&sdp->sd_log_thresh2));
138	return s;
139}
140
141static ssize_t fsname_show(struct gfs2_sbd *sdp, char *buf)
142{
143	return snprintf(buf, PAGE_SIZE, "%s\n", sdp->sd_fsname);
 
 
 
 
 
 
144}
145
146static ssize_t uuid_show(struct gfs2_sbd *sdp, char *buf)
147{
148	struct super_block *s = sdp->sd_vfs;
149
150	buf[0] = '\0';
151	if (uuid_is_null(&s->s_uuid))
152		return 0;
153	return snprintf(buf, PAGE_SIZE, "%pUB\n", &s->s_uuid);
154}
155
156static ssize_t freeze_show(struct gfs2_sbd *sdp, char *buf)
157{
158	struct super_block *sb = sdp->sd_vfs;
159	int frozen = (sb->s_writers.frozen == SB_UNFROZEN) ? 0 : 1;
160
161	return snprintf(buf, PAGE_SIZE, "%d\n", frozen);
162}
163
164static ssize_t freeze_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
165{
166	int error, n;
167
168	error = kstrtoint(buf, 0, &n);
169	if (error)
170		return error;
171
172	if (!capable(CAP_SYS_ADMIN))
173		return -EPERM;
174
175	switch (n) {
176	case 0:
177		error = thaw_super(sdp->sd_vfs, FREEZE_HOLDER_USERSPACE);
178		break;
179	case 1:
180		error = freeze_super(sdp->sd_vfs, FREEZE_HOLDER_USERSPACE);
181		break;
182	default:
183		return -EINVAL;
184	}
185
186	if (error) {
187		fs_warn(sdp, "freeze %d error %d\n", n, error);
188		return error;
189	}
190
191	return len;
192}
193
194static ssize_t withdraw_show(struct gfs2_sbd *sdp, char *buf)
195{
196	unsigned int b = gfs2_withdrawing_or_withdrawn(sdp);
197	return snprintf(buf, PAGE_SIZE, "%u\n", b);
198}
199
200static ssize_t withdraw_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
201{
202	int error, val;
203
204	if (!capable(CAP_SYS_ADMIN))
205		return -EPERM;
206
207	error = kstrtoint(buf, 0, &val);
208	if (error)
209		return error;
210
211	if (val != 1)
212		return -EINVAL;
213
214	gfs2_lm(sdp, "withdrawing from cluster at user's request\n");
215	gfs2_withdraw(sdp);
216
217	return len;
218}
219
220static ssize_t statfs_sync_store(struct gfs2_sbd *sdp, const char *buf,
221				 size_t len)
222{
223	int error, val;
224
225	if (!capable(CAP_SYS_ADMIN))
226		return -EPERM;
227
228	error = kstrtoint(buf, 0, &val);
229	if (error)
230		return error;
231
232	if (val != 1)
233		return -EINVAL;
234
235	gfs2_statfs_sync(sdp->sd_vfs, 0);
236	return len;
237}
238
239static ssize_t quota_sync_store(struct gfs2_sbd *sdp, const char *buf,
240				size_t len)
241{
242	int error, val;
243
244	if (!capable(CAP_SYS_ADMIN))
245		return -EPERM;
246
247	error = kstrtoint(buf, 0, &val);
248	if (error)
249		return error;
250
251	if (val != 1)
252		return -EINVAL;
253
254	gfs2_quota_sync(sdp->sd_vfs, 0);
255	return len;
256}
257
258static ssize_t quota_refresh_user_store(struct gfs2_sbd *sdp, const char *buf,
259					size_t len)
260{
261	struct kqid qid;
262	int error;
263	u32 id;
264
265	if (!capable(CAP_SYS_ADMIN))
266		return -EPERM;
267
268	error = kstrtou32(buf, 0, &id);
269	if (error)
270		return error;
271
272	qid = make_kqid(current_user_ns(), USRQUOTA, id);
273	if (!qid_valid(qid))
274		return -EINVAL;
275
276	error = gfs2_quota_refresh(sdp, qid);
277	return error ? error : len;
278}
279
280static ssize_t quota_refresh_group_store(struct gfs2_sbd *sdp, const char *buf,
281					 size_t len)
282{
283	struct kqid qid;
284	int error;
285	u32 id;
286
287	if (!capable(CAP_SYS_ADMIN))
288		return -EPERM;
289
290	error = kstrtou32(buf, 0, &id);
291	if (error)
292		return error;
293
294	qid = make_kqid(current_user_ns(), GRPQUOTA, id);
295	if (!qid_valid(qid))
296		return -EINVAL;
297
298	error = gfs2_quota_refresh(sdp, qid);
299	return error ? error : len;
300}
301
302static ssize_t demote_rq_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
303{
304	struct gfs2_glock *gl;
305	const struct gfs2_glock_operations *glops;
306	unsigned int glmode;
307	unsigned int gltype;
308	unsigned long long glnum;
309	char mode[16];
310	int rv;
311
312	if (!capable(CAP_SYS_ADMIN))
313		return -EPERM;
314
315	rv = sscanf(buf, "%u:%llu %15s", &gltype, &glnum,
316		    mode);
317	if (rv != 3)
318		return -EINVAL;
319
320	if (strcmp(mode, "EX") == 0)
321		glmode = LM_ST_UNLOCKED;
322	else if ((strcmp(mode, "CW") == 0) || (strcmp(mode, "DF") == 0))
323		glmode = LM_ST_DEFERRED;
324	else if ((strcmp(mode, "PR") == 0) || (strcmp(mode, "SH") == 0))
325		glmode = LM_ST_SHARED;
326	else
327		return -EINVAL;
328
329	if (gltype > LM_TYPE_JOURNAL)
330		return -EINVAL;
331	if (gltype == LM_TYPE_NONDISK && glnum == GFS2_FREEZE_LOCK)
332		glops = &gfs2_freeze_glops;
333	else
334		glops = gfs2_glops_list[gltype];
335	if (glops == NULL)
336		return -EINVAL;
337	if (!test_and_set_bit(SDF_DEMOTE, &sdp->sd_flags))
338		fs_info(sdp, "demote interface used\n");
339	rv = gfs2_glock_get(sdp, glnum, glops, NO_CREATE, &gl);
340	if (rv)
341		return rv;
342	gfs2_glock_cb(gl, glmode);
343	gfs2_glock_put(gl);
344	return len;
345}
346
347
348#define GFS2_ATTR(name, mode, show, store) \
349static struct gfs2_attr gfs2_attr_##name = __ATTR(name, mode, show, store)
350
351GFS2_ATTR(id,                  0444, id_show,       NULL);
352GFS2_ATTR(fsname,              0444, fsname_show,   NULL);
353GFS2_ATTR(uuid,                0444, uuid_show,     NULL);
354GFS2_ATTR(freeze,              0644, freeze_show,   freeze_store);
355GFS2_ATTR(withdraw,            0644, withdraw_show, withdraw_store);
356GFS2_ATTR(statfs_sync,         0200, NULL,          statfs_sync_store);
357GFS2_ATTR(quota_sync,          0200, NULL,          quota_sync_store);
358GFS2_ATTR(quota_refresh_user,  0200, NULL,          quota_refresh_user_store);
359GFS2_ATTR(quota_refresh_group, 0200, NULL,          quota_refresh_group_store);
360GFS2_ATTR(demote_rq,           0200, NULL,	    demote_rq_store);
361GFS2_ATTR(status,              0400, status_show,   NULL);
362
363static struct attribute *gfs2_attrs[] = {
364	&gfs2_attr_id.attr,
365	&gfs2_attr_fsname.attr,
366	&gfs2_attr_uuid.attr,
367	&gfs2_attr_freeze.attr,
368	&gfs2_attr_withdraw.attr,
369	&gfs2_attr_statfs_sync.attr,
370	&gfs2_attr_quota_sync.attr,
371	&gfs2_attr_quota_refresh_user.attr,
372	&gfs2_attr_quota_refresh_group.attr,
373	&gfs2_attr_demote_rq.attr,
374	&gfs2_attr_status.attr,
375	NULL,
376};
377ATTRIBUTE_GROUPS(gfs2);
378
379static void gfs2_sbd_release(struct kobject *kobj)
380{
381	struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
382
383	complete(&sdp->sd_kobj_unregister);
384}
385
386static struct kobj_type gfs2_ktype = {
387	.release = gfs2_sbd_release,
388	.default_groups = gfs2_groups,
389	.sysfs_ops     = &gfs2_attr_ops,
390};
391
392
393/*
394 * lock_module. Originally from lock_dlm
395 */
396
397static ssize_t proto_name_show(struct gfs2_sbd *sdp, char *buf)
398{
399	const struct lm_lockops *ops = sdp->sd_lockstruct.ls_ops;
400	return sprintf(buf, "%s\n", ops->lm_proto_name);
401}
402
403static ssize_t block_show(struct gfs2_sbd *sdp, char *buf)
404{
405	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
406	ssize_t ret;
407	int val = 0;
408
409	if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))
410		val = 1;
411	ret = sprintf(buf, "%d\n", val);
412	return ret;
413}
414
415static ssize_t block_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
416{
417	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
418	int ret, val;
 
419
420	ret = kstrtoint(buf, 0, &val);
421	if (ret)
422		return ret;
423
424	if (val == 1)
425		set_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
426	else if (val == 0) {
427		clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
428		smp_mb__after_atomic();
429		gfs2_glock_thaw(sdp);
430	} else {
431		return -EINVAL;
432	}
433	return len;
434}
435
436static ssize_t wdack_show(struct gfs2_sbd *sdp, char *buf)
437{
438	int val = completion_done(&sdp->sd_wdack) ? 1 : 0;
439
440	return sprintf(buf, "%d\n", val);
441}
442
443static ssize_t wdack_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
444{
445	int ret, val;
 
446
447	ret = kstrtoint(buf, 0, &val);
448	if (ret)
449		return ret;
450
451	if ((val == 1) &&
452	    !strcmp(sdp->sd_lockstruct.ls_ops->lm_proto_name, "lock_dlm"))
453		complete(&sdp->sd_wdack);
454	else
455		return -EINVAL;
456	return len;
457}
458
459static ssize_t lkfirst_show(struct gfs2_sbd *sdp, char *buf)
460{
461	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
462	return sprintf(buf, "%d\n", ls->ls_first);
463}
464
465static ssize_t lkfirst_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
466{
467	unsigned first;
468	int rv;
469
470	rv = sscanf(buf, "%u", &first);
471	if (rv != 1 || first > 1)
472		return -EINVAL;
473	rv = wait_for_completion_killable(&sdp->sd_locking_init);
474	if (rv)
475		return rv;
476	spin_lock(&sdp->sd_jindex_spin);
477	rv = -EBUSY;
478	if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
479		goto out;
480	rv = -EINVAL;
481	if (sdp->sd_args.ar_spectator)
482		goto out;
483	if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
484		goto out;
485	sdp->sd_lockstruct.ls_first = first;
486	rv = 0;
487out:
488        spin_unlock(&sdp->sd_jindex_spin);
489        return rv ? rv : len;
490}
491
492static ssize_t first_done_show(struct gfs2_sbd *sdp, char *buf)
493{
494	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
495	return sprintf(buf, "%d\n", !!test_bit(DFL_FIRST_MOUNT_DONE, &ls->ls_recover_flags));
496}
497
498int gfs2_recover_set(struct gfs2_sbd *sdp, unsigned jid)
499{
500	struct gfs2_jdesc *jd;
501	int rv;
502
503	/* Wait for our primary journal to be initialized */
504	wait_for_completion(&sdp->sd_journal_ready);
505
506	spin_lock(&sdp->sd_jindex_spin);
507	rv = -EBUSY;
508	/**
509	 * If we're a spectator, we use journal0, but it's not really ours.
510	 * So we need to wait for its recovery too. If we skip it we'd never
511	 * queue work to the recovery workqueue, and so its completion would
512	 * never clear the DFL_BLOCK_LOCKS flag, so all our locks would
513	 * permanently stop working.
514	 */
515	if (!sdp->sd_jdesc)
516		goto out;
517	if (sdp->sd_jdesc->jd_jid == jid && !sdp->sd_args.ar_spectator)
518		goto out;
519	rv = -ENOENT;
520	list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
521		if (jd->jd_jid != jid && !sdp->sd_args.ar_spectator)
522			continue;
523		rv = gfs2_recover_journal(jd, false);
524		break;
525	}
526out:
527	spin_unlock(&sdp->sd_jindex_spin);
528	return rv;
529}
530
531static ssize_t recover_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
532{
533	unsigned jid;
534	int rv;
535
536	rv = sscanf(buf, "%u", &jid);
537	if (rv != 1)
538		return -EINVAL;
539
540	if (test_bit(SDF_NORECOVERY, &sdp->sd_flags)) {
541		rv = -ESHUTDOWN;
542		goto out;
543	}
544
545	rv = gfs2_recover_set(sdp, jid);
546out:
547	return rv ? rv : len;
548}
549
550static ssize_t recover_done_show(struct gfs2_sbd *sdp, char *buf)
551{
552	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
553	return sprintf(buf, "%d\n", ls->ls_recover_jid_done);
554}
555
556static ssize_t recover_status_show(struct gfs2_sbd *sdp, char *buf)
557{
558	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
559	return sprintf(buf, "%d\n", ls->ls_recover_jid_status);
560}
561
562static ssize_t jid_show(struct gfs2_sbd *sdp, char *buf)
563{
564	return sprintf(buf, "%d\n", sdp->sd_lockstruct.ls_jid);
565}
566
567static ssize_t jid_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
568{
569        int jid;
570	int rv;
571
572	rv = sscanf(buf, "%d", &jid);
573	if (rv != 1)
574		return -EINVAL;
575	rv = wait_for_completion_killable(&sdp->sd_locking_init);
576	if (rv)
577		return rv;
578	spin_lock(&sdp->sd_jindex_spin);
579	rv = -EINVAL;
580	if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
581		goto out;
582	rv = -EBUSY;
583	if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
584		goto out;
585	rv = 0;
586	if (sdp->sd_args.ar_spectator && jid > 0)
587		rv = jid = -EINVAL;
588	sdp->sd_lockstruct.ls_jid = jid;
589	clear_bit(SDF_NOJOURNALID, &sdp->sd_flags);
590	smp_mb__after_atomic();
591	wake_up_bit(&sdp->sd_flags, SDF_NOJOURNALID);
592out:
593	spin_unlock(&sdp->sd_jindex_spin);
594	return rv ? rv : len;
595}
596
597#define GDLM_ATTR(_name,_mode,_show,_store) \
598static struct gfs2_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
599
600GDLM_ATTR(proto_name,		0444, proto_name_show,		NULL);
601GDLM_ATTR(block,		0644, block_show,		block_store);
602GDLM_ATTR(withdraw,		0644, wdack_show,		wdack_store);
603GDLM_ATTR(jid,			0644, jid_show,			jid_store);
604GDLM_ATTR(first,		0644, lkfirst_show,		lkfirst_store);
605GDLM_ATTR(first_done,		0444, first_done_show,		NULL);
606GDLM_ATTR(recover,		0600, NULL,			recover_store);
607GDLM_ATTR(recover_done,		0444, recover_done_show,	NULL);
608GDLM_ATTR(recover_status,	0444, recover_status_show,	NULL);
609
610static struct attribute *lock_module_attrs[] = {
611	&gdlm_attr_proto_name.attr,
612	&gdlm_attr_block.attr,
613	&gdlm_attr_withdraw.attr,
614	&gdlm_attr_jid.attr,
615	&gdlm_attr_first.attr,
616	&gdlm_attr_first_done.attr,
617	&gdlm_attr_recover.attr,
618	&gdlm_attr_recover_done.attr,
619	&gdlm_attr_recover_status.attr,
620	NULL,
621};
622
623/*
624 * get and set struct gfs2_tune fields
625 */
626
627static ssize_t quota_scale_show(struct gfs2_sbd *sdp, char *buf)
628{
629	return snprintf(buf, PAGE_SIZE, "%u %u\n",
630			sdp->sd_tune.gt_quota_scale_num,
631			sdp->sd_tune.gt_quota_scale_den);
632}
633
634static ssize_t quota_scale_store(struct gfs2_sbd *sdp, const char *buf,
635				 size_t len)
636{
637	struct gfs2_tune *gt = &sdp->sd_tune;
638	unsigned int x, y;
639
640	if (!capable(CAP_SYS_ADMIN))
641		return -EPERM;
642
643	if (sscanf(buf, "%u %u", &x, &y) != 2 || !y)
644		return -EINVAL;
645
646	spin_lock(&gt->gt_spin);
647	gt->gt_quota_scale_num = x;
648	gt->gt_quota_scale_den = y;
649	spin_unlock(&gt->gt_spin);
650	return len;
651}
652
653static ssize_t tune_set(struct gfs2_sbd *sdp, unsigned int *field,
654			int check_zero, const char *buf, size_t len)
655{
656	struct gfs2_tune *gt = &sdp->sd_tune;
657	unsigned int x;
658	int error;
659
660	if (!capable(CAP_SYS_ADMIN))
661		return -EPERM;
662
663	error = kstrtouint(buf, 0, &x);
664	if (error)
665		return error;
666
667	if (check_zero && !x)
668		return -EINVAL;
669
670	spin_lock(&gt->gt_spin);
671	*field = x;
672	spin_unlock(&gt->gt_spin);
673	return len;
674}
675
676#define TUNE_ATTR_3(name, show, store)                                        \
677static struct gfs2_attr tune_attr_##name = __ATTR(name, 0644, show, store)
678
679#define TUNE_ATTR_2(name, store)                                              \
680static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf)                   \
681{                                                                             \
682	return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name);      \
683}                                                                             \
684TUNE_ATTR_3(name, name##_show, store)
685
686#define TUNE_ATTR(name, check_zero)                                           \
687static ssize_t name##_store(struct gfs2_sbd *sdp, const char *buf, size_t len)\
688{                                                                             \
689	return tune_set(sdp, &sdp->sd_tune.gt_##name, check_zero, buf, len);  \
690}                                                                             \
691TUNE_ATTR_2(name, name##_store)
692
693TUNE_ATTR(quota_warn_period, 0);
694TUNE_ATTR(quota_quantum, 0);
695TUNE_ATTR(max_readahead, 0);
696TUNE_ATTR(complain_secs, 0);
697TUNE_ATTR(statfs_slow, 0);
698TUNE_ATTR(new_files_jdata, 0);
699TUNE_ATTR(statfs_quantum, 1);
700TUNE_ATTR_3(quota_scale, quota_scale_show, quota_scale_store);
701
702static struct attribute *tune_attrs[] = {
703	&tune_attr_quota_warn_period.attr,
704	&tune_attr_quota_quantum.attr,
705	&tune_attr_max_readahead.attr,
706	&tune_attr_complain_secs.attr,
707	&tune_attr_statfs_slow.attr,
708	&tune_attr_statfs_quantum.attr,
709	&tune_attr_quota_scale.attr,
710	&tune_attr_new_files_jdata.attr,
711	NULL,
712};
713
714static const struct attribute_group tune_group = {
715	.name = "tune",
716	.attrs = tune_attrs,
717};
718
719static const struct attribute_group lock_module_group = {
720	.name = "lock_module",
721	.attrs = lock_module_attrs,
722};
723
724int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
725{
726	struct super_block *sb = sdp->sd_vfs;
727	int error;
728	char ro[20];
729	char spectator[20];
730	char *envp[] = { ro, spectator, NULL };
 
731
732	sprintf(ro, "RDONLY=%d", sb_rdonly(sb));
733	sprintf(spectator, "SPECTATOR=%d", sdp->sd_args.ar_spectator ? 1 : 0);
734
735	init_completion(&sdp->sd_kobj_unregister);
736	sdp->sd_kobj.kset = gfs2_kset;
737	error = kobject_init_and_add(&sdp->sd_kobj, &gfs2_ktype, NULL,
738				     "%s", sdp->sd_table_name);
739	if (error)
740		goto fail_reg;
741
 
 
742	error = sysfs_create_group(&sdp->sd_kobj, &tune_group);
743	if (error)
744		goto fail_reg;
745
746	error = sysfs_create_group(&sdp->sd_kobj, &lock_module_group);
747	if (error)
748		goto fail_tune;
749
750	error = sysfs_create_link(&sdp->sd_kobj,
751				  &disk_to_dev(sb->s_bdev->bd_disk)->kobj,
752				  "device");
753	if (error)
754		goto fail_lock_module;
755
756	kobject_uevent_env(&sdp->sd_kobj, KOBJ_ADD, envp);
757	return 0;
758
759fail_lock_module:
760	sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
761fail_tune:
762	sysfs_remove_group(&sdp->sd_kobj, &tune_group);
763fail_reg:
764	fs_err(sdp, "error %d adding sysfs files\n", error);
765	kobject_put(&sdp->sd_kobj);
766	wait_for_completion(&sdp->sd_kobj_unregister);
 
 
 
767	sb->s_fs_info = NULL;
768	return error;
769}
770
771void gfs2_sys_fs_del(struct gfs2_sbd *sdp)
772{
773	sysfs_remove_link(&sdp->sd_kobj, "device");
774	sysfs_remove_group(&sdp->sd_kobj, &tune_group);
775	sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
776	kobject_put(&sdp->sd_kobj);
777	wait_for_completion(&sdp->sd_kobj_unregister);
778}
779
780static int gfs2_uevent(const struct kobject *kobj, struct kobj_uevent_env *env)
 
781{
782	const struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
783	const struct super_block *s = sdp->sd_vfs;
 
784
785	add_uevent_var(env, "LOCKTABLE=%s", sdp->sd_table_name);
786	add_uevent_var(env, "LOCKPROTO=%s", sdp->sd_proto_name);
787	if (!test_bit(SDF_NOJOURNALID, &sdp->sd_flags))
788		add_uevent_var(env, "JOURNALID=%d", sdp->sd_lockstruct.ls_jid);
789	if (!uuid_is_null(&s->s_uuid))
790		add_uevent_var(env, "UUID=%pUB", &s->s_uuid);
791	return 0;
792}
793
794static const struct kset_uevent_ops gfs2_uevent_ops = {
795	.uevent = gfs2_uevent,
796};
797
798int gfs2_sys_init(void)
799{
800	gfs2_kset = kset_create_and_add("gfs2", &gfs2_uevent_ops, fs_kobj);
801	if (!gfs2_kset)
802		return -ENOMEM;
803	return 0;
804}
805
806void gfs2_sys_uninit(void)
807{
808	kset_unregister(gfs2_kset);
809}
810