Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (c) 2014 Red Hat, Inc.
  4 * All Rights Reserved.
 
 
 
 
 
 
 
 
 
 
 
 
 
  5 */
  6
  7#include "xfs.h"
  8#include "xfs_shared.h"
  9#include "xfs_format.h"
 10#include "xfs_log_format.h"
 11#include "xfs_trans_resv.h"
 12#include "xfs_sysfs.h"
 13#include "xfs_log.h"
 14#include "xfs_log_priv.h"
 
 15#include "xfs_mount.h"
 16
 17struct xfs_sysfs_attr {
 18	struct attribute attr;
 19	ssize_t (*show)(struct kobject *kobject, char *buf);
 20	ssize_t (*store)(struct kobject *kobject, const char *buf,
 21			 size_t count);
 22};
 23
 24static inline struct xfs_sysfs_attr *
 25to_attr(struct attribute *attr)
 26{
 27	return container_of(attr, struct xfs_sysfs_attr, attr);
 28}
 29
 30#define XFS_SYSFS_ATTR_RW(name) \
 31	static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_RW(name)
 32#define XFS_SYSFS_ATTR_RO(name) \
 33	static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_RO(name)
 34#define XFS_SYSFS_ATTR_WO(name) \
 35	static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_WO(name)
 36
 37#define ATTR_LIST(name) &xfs_sysfs_attr_##name.attr
 38
 39STATIC ssize_t
 40xfs_sysfs_object_show(
 41	struct kobject		*kobject,
 42	struct attribute	*attr,
 43	char			*buf)
 44{
 45	struct xfs_sysfs_attr *xfs_attr = to_attr(attr);
 46
 47	return xfs_attr->show ? xfs_attr->show(kobject, buf) : 0;
 48}
 49
 50STATIC ssize_t
 51xfs_sysfs_object_store(
 52	struct kobject		*kobject,
 53	struct attribute	*attr,
 54	const char		*buf,
 55	size_t			count)
 56{
 57	struct xfs_sysfs_attr *xfs_attr = to_attr(attr);
 58
 59	return xfs_attr->store ? xfs_attr->store(kobject, buf, count) : 0;
 60}
 61
 62static const struct sysfs_ops xfs_sysfs_ops = {
 63	.show = xfs_sysfs_object_show,
 64	.store = xfs_sysfs_object_store,
 65};
 66
 67static struct attribute *xfs_mp_attrs[] = {
 68	NULL,
 69};
 70ATTRIBUTE_GROUPS(xfs_mp);
 71
 72const struct kobj_type xfs_mp_ktype = {
 73	.release = xfs_sysfs_release,
 74	.sysfs_ops = &xfs_sysfs_ops,
 75	.default_groups = xfs_mp_groups,
 76};
 
 
 77
 78#ifdef DEBUG
 79/* debug */
 80
 81STATIC ssize_t
 82bug_on_assert_store(
 83	struct kobject		*kobject,
 84	const char		*buf,
 85	size_t			count)
 86{
 
 87	int			ret;
 88	int			val;
 89
 90	ret = kstrtoint(buf, 0, &val);
 91	if (ret)
 92		return ret;
 93
 94	if (val == 1)
 95		xfs_globals.bug_on_assert = true;
 96	else if (val == 0)
 97		xfs_globals.bug_on_assert = false;
 98	else
 99		return -EINVAL;
100
101	return count;
102}
103
104STATIC ssize_t
105bug_on_assert_show(
106	struct kobject		*kobject,
107	char			*buf)
108{
109	return sysfs_emit(buf, "%d\n", xfs_globals.bug_on_assert);
110}
111XFS_SYSFS_ATTR_RW(bug_on_assert);
112
113STATIC ssize_t
114log_recovery_delay_store(
115	struct kobject	*kobject,
116	const char	*buf,
117	size_t		count)
118{
119	int		ret;
120	int		val;
121
122	ret = kstrtoint(buf, 0, &val);
123	if (ret)
124		return ret;
125
126	if (val < 0 || val > 60)
127		return -EINVAL;
128
129	xfs_globals.log_recovery_delay = val;
130
131	return count;
132}
133
134STATIC ssize_t
135log_recovery_delay_show(
136	struct kobject	*kobject,
137	char		*buf)
138{
139	return sysfs_emit(buf, "%d\n", xfs_globals.log_recovery_delay);
140}
141XFS_SYSFS_ATTR_RW(log_recovery_delay);
142
143STATIC ssize_t
144mount_delay_store(
145	struct kobject	*kobject,
146	const char	*buf,
147	size_t		count)
148{
149	int		ret;
150	int		val;
151
152	ret = kstrtoint(buf, 0, &val);
153	if (ret)
154		return ret;
155
156	if (val < 0 || val > 60)
157		return -EINVAL;
158
159	xfs_globals.mount_delay = val;
160
161	return count;
162}
163
164STATIC ssize_t
165mount_delay_show(
166	struct kobject	*kobject,
167	char		*buf)
168{
169	return sysfs_emit(buf, "%d\n", xfs_globals.mount_delay);
170}
171XFS_SYSFS_ATTR_RW(mount_delay);
172
173static ssize_t
174always_cow_store(
175	struct kobject	*kobject,
176	const char	*buf,
177	size_t		count)
178{
179	ssize_t		ret;
180
181	ret = kstrtobool(buf, &xfs_globals.always_cow);
182	if (ret < 0)
183		return ret;
184	return count;
185}
186
187static ssize_t
188always_cow_show(
189	struct kobject	*kobject,
190	char		*buf)
191{
192	return sysfs_emit(buf, "%d\n", xfs_globals.always_cow);
193}
194XFS_SYSFS_ATTR_RW(always_cow);
195
 
196#ifdef DEBUG
197/*
198 * Override how many threads the parallel work queue is allowed to create.
199 * This has to be a debug-only global (instead of an errortag) because one of
200 * the main users of parallel workqueues is mount time quotacheck.
201 */
202STATIC ssize_t
203pwork_threads_store(
204	struct kobject	*kobject,
205	const char	*buf,
206	size_t		count)
207{
208	int		ret;
209	int		val;
210
211	ret = kstrtoint(buf, 0, &val);
212	if (ret)
213		return ret;
214
215	if (val < -1 || val > num_possible_cpus())
216		return -EINVAL;
217
218	xfs_globals.pwork_threads = val;
219
220	return count;
221}
222
223STATIC ssize_t
224pwork_threads_show(
225	struct kobject	*kobject,
226	char		*buf)
227{
228	return sysfs_emit(buf, "%d\n", xfs_globals.pwork_threads);
229}
230XFS_SYSFS_ATTR_RW(pwork_threads);
231
232/*
233 * The "LARP" (Logged extended Attribute Recovery Persistence) debugging knob
234 * sets the XFS_DA_OP_LOGGED flag on all xfs_attr_set operations performed on
235 * V5 filesystems.  As a result, the intermediate progress of all setxattr and
236 * removexattr operations are tracked via the log and can be restarted during
237 * recovery.  This is useful for testing xattr recovery prior to merging of the
238 * parent pointer feature which requires it to maintain consistency, and may be
239 * enabled for userspace xattrs in the future.
240 */
241static ssize_t
242larp_store(
243	struct kobject	*kobject,
244	const char	*buf,
245	size_t		count)
246{
247	ssize_t		ret;
248
249	ret = kstrtobool(buf, &xfs_globals.larp);
250	if (ret < 0)
251		return ret;
252	return count;
253}
254
255STATIC ssize_t
256larp_show(
257	struct kobject	*kobject,
258	char		*buf)
259{
260	return snprintf(buf, PAGE_SIZE, "%d\n", xfs_globals.larp);
261}
262XFS_SYSFS_ATTR_RW(larp);
263#endif /* DEBUG */
264
265STATIC ssize_t
266bload_leaf_slack_store(
267	struct kobject	*kobject,
268	const char	*buf,
269	size_t		count)
270{
271	int		ret;
272	int		val;
273
274	ret = kstrtoint(buf, 0, &val);
275	if (ret)
276		return ret;
277
278	xfs_globals.bload_leaf_slack = val;
279	return count;
280}
281
282STATIC ssize_t
283bload_leaf_slack_show(
284	struct kobject	*kobject,
285	char		*buf)
286{
287	return snprintf(buf, PAGE_SIZE, "%d\n", xfs_globals.bload_leaf_slack);
288}
289XFS_SYSFS_ATTR_RW(bload_leaf_slack);
290
291STATIC ssize_t
292bload_node_slack_store(
293	struct kobject	*kobject,
294	const char	*buf,
295	size_t		count)
296{
297	int		ret;
298	int		val;
299
300	ret = kstrtoint(buf, 0, &val);
301	if (ret)
302		return ret;
303
304	xfs_globals.bload_node_slack = val;
305	return count;
306}
307
308STATIC ssize_t
309bload_node_slack_show(
310	struct kobject	*kobject,
311	char		*buf)
312{
313	return snprintf(buf, PAGE_SIZE, "%d\n", xfs_globals.bload_node_slack);
314}
315XFS_SYSFS_ATTR_RW(bload_node_slack);
316
317static struct attribute *xfs_dbg_attrs[] = {
318	ATTR_LIST(bug_on_assert),
319	ATTR_LIST(log_recovery_delay),
320	ATTR_LIST(mount_delay),
321	ATTR_LIST(always_cow),
322#ifdef DEBUG
323	ATTR_LIST(pwork_threads),
324	ATTR_LIST(larp),
325#endif
326	ATTR_LIST(bload_leaf_slack),
327	ATTR_LIST(bload_node_slack),
328	NULL,
329};
330ATTRIBUTE_GROUPS(xfs_dbg);
331
332const struct kobj_type xfs_dbg_ktype = {
333	.release = xfs_sysfs_release,
334	.sysfs_ops = &xfs_sysfs_ops,
335	.default_groups = xfs_dbg_groups,
336};
337
338#endif /* DEBUG */
339
340/* stats */
341
342static inline struct xstats *
343to_xstats(struct kobject *kobject)
344{
345	struct xfs_kobj *kobj = to_kobj(kobject);
346
347	return container_of(kobj, struct xstats, xs_kobj);
348}
349
350STATIC ssize_t
351stats_show(
352	struct kobject	*kobject,
353	char		*buf)
354{
355	struct xstats	*stats = to_xstats(kobject);
356
357	return xfs_stats_format(stats->xs_stats, buf);
358}
359XFS_SYSFS_ATTR_RO(stats);
360
361STATIC ssize_t
362stats_clear_store(
363	struct kobject	*kobject,
364	const char	*buf,
365	size_t		count)
366{
367	int		ret;
368	int		val;
369	struct xstats	*stats = to_xstats(kobject);
370
371	ret = kstrtoint(buf, 0, &val);
372	if (ret)
373		return ret;
374
375	if (val != 1)
376		return -EINVAL;
377
378	xfs_stats_clearall(stats->xs_stats);
379	return count;
380}
381XFS_SYSFS_ATTR_WO(stats_clear);
382
383static struct attribute *xfs_stats_attrs[] = {
384	ATTR_LIST(stats),
385	ATTR_LIST(stats_clear),
386	NULL,
387};
388ATTRIBUTE_GROUPS(xfs_stats);
389
390const struct kobj_type xfs_stats_ktype = {
391	.release = xfs_sysfs_release,
392	.sysfs_ops = &xfs_sysfs_ops,
393	.default_groups = xfs_stats_groups,
394};
395
396/* xlog */
397
398static inline struct xlog *
399to_xlog(struct kobject *kobject)
400{
401	struct xfs_kobj *kobj = to_kobj(kobject);
402
403	return container_of(kobj, struct xlog, l_kobj);
404}
405
406STATIC ssize_t
407log_head_lsn_show(
408	struct kobject	*kobject,
409	char		*buf)
410{
411	int cycle;
412	int block;
413	struct xlog *log = to_xlog(kobject);
414
415	spin_lock(&log->l_icloglock);
416	cycle = log->l_curr_cycle;
417	block = log->l_curr_block;
418	spin_unlock(&log->l_icloglock);
419
420	return sysfs_emit(buf, "%d:%d\n", cycle, block);
421}
422XFS_SYSFS_ATTR_RO(log_head_lsn);
423
424STATIC ssize_t
425log_tail_lsn_show(
426	struct kobject	*kobject,
427	char		*buf)
428{
429	int cycle;
430	int block;
431	struct xlog *log = to_xlog(kobject);
432
433	xlog_crack_atomic_lsn(&log->l_tail_lsn, &cycle, &block);
434	return sysfs_emit(buf, "%d:%d\n", cycle, block);
435}
436XFS_SYSFS_ATTR_RO(log_tail_lsn);
437
438STATIC ssize_t
439reserve_grant_head_show(
440	struct kobject	*kobject,
441	char		*buf)
442
443{
444	int cycle;
445	int bytes;
446	struct xlog *log = to_xlog(kobject);
447
448	xlog_crack_grant_head(&log->l_reserve_head.grant, &cycle, &bytes);
449	return sysfs_emit(buf, "%d:%d\n", cycle, bytes);
450}
451XFS_SYSFS_ATTR_RO(reserve_grant_head);
452
453STATIC ssize_t
454write_grant_head_show(
455	struct kobject	*kobject,
456	char		*buf)
457{
458	int cycle;
459	int bytes;
460	struct xlog *log = to_xlog(kobject);
461
462	xlog_crack_grant_head(&log->l_write_head.grant, &cycle, &bytes);
463	return sysfs_emit(buf, "%d:%d\n", cycle, bytes);
464}
465XFS_SYSFS_ATTR_RO(write_grant_head);
466
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
467static struct attribute *xfs_log_attrs[] = {
468	ATTR_LIST(log_head_lsn),
469	ATTR_LIST(log_tail_lsn),
470	ATTR_LIST(reserve_grant_head),
471	ATTR_LIST(write_grant_head),
 
 
 
472	NULL,
473};
474ATTRIBUTE_GROUPS(xfs_log);
475
476const struct kobj_type xfs_log_ktype = {
477	.release = xfs_sysfs_release,
478	.sysfs_ops = &xfs_sysfs_ops,
479	.default_groups = xfs_log_groups,
480};
481
482/*
483 * Metadata IO error configuration
484 *
485 * The sysfs structure here is:
486 *	...xfs/<dev>/error/<class>/<errno>/<error_attrs>
487 *
488 * where <class> allows us to discriminate between data IO and metadata IO,
489 * and any other future type of IO (e.g. special inode or directory error
490 * handling) we care to support.
491 */
492static inline struct xfs_error_cfg *
493to_error_cfg(struct kobject *kobject)
494{
495	struct xfs_kobj *kobj = to_kobj(kobject);
496	return container_of(kobj, struct xfs_error_cfg, kobj);
497}
498
499static inline struct xfs_mount *
500err_to_mp(struct kobject *kobject)
501{
502	struct xfs_kobj *kobj = to_kobj(kobject);
503	return container_of(kobj, struct xfs_mount, m_error_kobj);
504}
505
506static ssize_t
507max_retries_show(
508	struct kobject	*kobject,
509	char		*buf)
510{
511	int		retries;
512	struct xfs_error_cfg *cfg = to_error_cfg(kobject);
513
514	if (cfg->max_retries == XFS_ERR_RETRY_FOREVER)
515		retries = -1;
516	else
517		retries = cfg->max_retries;
518
519	return sysfs_emit(buf, "%d\n", retries);
520}
521
522static ssize_t
523max_retries_store(
524	struct kobject	*kobject,
525	const char	*buf,
526	size_t		count)
527{
528	struct xfs_error_cfg *cfg = to_error_cfg(kobject);
529	int		ret;
530	int		val;
531
532	ret = kstrtoint(buf, 0, &val);
533	if (ret)
534		return ret;
535
536	if (val < -1)
537		return -EINVAL;
538
539	if (val == -1)
540		cfg->max_retries = XFS_ERR_RETRY_FOREVER;
541	else
542		cfg->max_retries = val;
543	return count;
544}
545XFS_SYSFS_ATTR_RW(max_retries);
546
547static ssize_t
548retry_timeout_seconds_show(
549	struct kobject	*kobject,
550	char		*buf)
551{
552	int		timeout;
553	struct xfs_error_cfg *cfg = to_error_cfg(kobject);
554
555	if (cfg->retry_timeout == XFS_ERR_RETRY_FOREVER)
556		timeout = -1;
557	else
558		timeout = jiffies_to_msecs(cfg->retry_timeout) / MSEC_PER_SEC;
559
560	return sysfs_emit(buf, "%d\n", timeout);
561}
562
563static ssize_t
564retry_timeout_seconds_store(
565	struct kobject	*kobject,
566	const char	*buf,
567	size_t		count)
568{
569	struct xfs_error_cfg *cfg = to_error_cfg(kobject);
570	int		ret;
571	int		val;
572
573	ret = kstrtoint(buf, 0, &val);
574	if (ret)
575		return ret;
576
577	/* 1 day timeout maximum, -1 means infinite */
578	if (val < -1 || val > 86400)
579		return -EINVAL;
580
581	if (val == -1)
582		cfg->retry_timeout = XFS_ERR_RETRY_FOREVER;
583	else {
584		cfg->retry_timeout = msecs_to_jiffies(val * MSEC_PER_SEC);
585		ASSERT(msecs_to_jiffies(val * MSEC_PER_SEC) < LONG_MAX);
586	}
587	return count;
588}
589XFS_SYSFS_ATTR_RW(retry_timeout_seconds);
590
591static ssize_t
592fail_at_unmount_show(
593	struct kobject	*kobject,
594	char		*buf)
595{
596	struct xfs_mount	*mp = err_to_mp(kobject);
597
598	return sysfs_emit(buf, "%d\n", mp->m_fail_unmount);
599}
600
601static ssize_t
602fail_at_unmount_store(
603	struct kobject	*kobject,
604	const char	*buf,
605	size_t		count)
606{
607	struct xfs_mount	*mp = err_to_mp(kobject);
608	int		ret;
609	int		val;
610
611	ret = kstrtoint(buf, 0, &val);
612	if (ret)
613		return ret;
614
615	if (val < 0 || val > 1)
616		return -EINVAL;
617
618	mp->m_fail_unmount = val;
619	return count;
620}
621XFS_SYSFS_ATTR_RW(fail_at_unmount);
622
623static struct attribute *xfs_error_attrs[] = {
624	ATTR_LIST(max_retries),
625	ATTR_LIST(retry_timeout_seconds),
626	NULL,
627};
628ATTRIBUTE_GROUPS(xfs_error);
629
630static const struct kobj_type xfs_error_cfg_ktype = {
 
631	.release = xfs_sysfs_release,
632	.sysfs_ops = &xfs_sysfs_ops,
633	.default_groups = xfs_error_groups,
634};
635
636static const struct kobj_type xfs_error_ktype = {
637	.release = xfs_sysfs_release,
638	.sysfs_ops = &xfs_sysfs_ops,
639};
640
641/*
642 * Error initialization tables. These need to be ordered in the same
643 * order as the enums used to index the array. All class init tables need to
644 * define a "default" behaviour as the first entry, all other entries can be
645 * empty.
646 */
647struct xfs_error_init {
648	char		*name;
649	int		max_retries;
650	int		retry_timeout;	/* in seconds */
651};
652
653static const struct xfs_error_init xfs_error_meta_init[XFS_ERR_ERRNO_MAX] = {
654	{ .name = "default",
655	  .max_retries = XFS_ERR_RETRY_FOREVER,
656	  .retry_timeout = XFS_ERR_RETRY_FOREVER,
657	},
658	{ .name = "EIO",
659	  .max_retries = XFS_ERR_RETRY_FOREVER,
660	  .retry_timeout = XFS_ERR_RETRY_FOREVER,
661	},
662	{ .name = "ENOSPC",
663	  .max_retries = XFS_ERR_RETRY_FOREVER,
664	  .retry_timeout = XFS_ERR_RETRY_FOREVER,
665	},
666	{ .name = "ENODEV",
667	  .max_retries = 0,	/* We can't recover from devices disappearing */
668	  .retry_timeout = 0,
669	},
670};
671
672static int
673xfs_error_sysfs_init_class(
674	struct xfs_mount	*mp,
675	int			class,
676	const char		*parent_name,
677	struct xfs_kobj		*parent_kobj,
678	const struct xfs_error_init init[])
679{
680	struct xfs_error_cfg	*cfg;
681	int			error;
682	int			i;
683
684	ASSERT(class < XFS_ERR_CLASS_MAX);
685
686	error = xfs_sysfs_init(parent_kobj, &xfs_error_ktype,
687				&mp->m_error_kobj, parent_name);
688	if (error)
689		return error;
690
691	for (i = 0; i < XFS_ERR_ERRNO_MAX; i++) {
692		cfg = &mp->m_error_cfg[class][i];
693		error = xfs_sysfs_init(&cfg->kobj, &xfs_error_cfg_ktype,
694					parent_kobj, init[i].name);
695		if (error)
696			goto out_error;
697
698		cfg->max_retries = init[i].max_retries;
699		if (init[i].retry_timeout == XFS_ERR_RETRY_FOREVER)
700			cfg->retry_timeout = XFS_ERR_RETRY_FOREVER;
701		else
702			cfg->retry_timeout = msecs_to_jiffies(
703					init[i].retry_timeout * MSEC_PER_SEC);
704	}
705	return 0;
706
707out_error:
708	/* unwind the entries that succeeded */
709	for (i--; i >= 0; i--) {
710		cfg = &mp->m_error_cfg[class][i];
711		xfs_sysfs_del(&cfg->kobj);
712	}
713	xfs_sysfs_del(parent_kobj);
714	return error;
715}
716
717int
718xfs_error_sysfs_init(
719	struct xfs_mount	*mp)
720{
721	int			error;
722
723	/* .../xfs/<dev>/error/ */
724	error = xfs_sysfs_init(&mp->m_error_kobj, &xfs_error_ktype,
725				&mp->m_kobj, "error");
726	if (error)
727		return error;
728
729	error = sysfs_create_file(&mp->m_error_kobj.kobject,
730				  ATTR_LIST(fail_at_unmount));
731
732	if (error)
733		goto out_error;
734
735	/* .../xfs/<dev>/error/metadata/ */
736	error = xfs_error_sysfs_init_class(mp, XFS_ERR_METADATA,
737				"metadata", &mp->m_error_meta_kobj,
738				xfs_error_meta_init);
739	if (error)
740		goto out_error;
741
742	return 0;
743
744out_error:
745	xfs_sysfs_del(&mp->m_error_kobj);
746	return error;
747}
748
749void
750xfs_error_sysfs_del(
751	struct xfs_mount	*mp)
752{
753	struct xfs_error_cfg	*cfg;
754	int			i, j;
755
756	for (i = 0; i < XFS_ERR_CLASS_MAX; i++) {
757		for (j = 0; j < XFS_ERR_ERRNO_MAX; j++) {
758			cfg = &mp->m_error_cfg[i][j];
759
760			xfs_sysfs_del(&cfg->kobj);
761		}
762	}
763	xfs_sysfs_del(&mp->m_error_meta_kobj);
764	xfs_sysfs_del(&mp->m_error_kobj);
765}
766
767struct xfs_error_cfg *
768xfs_error_get_cfg(
769	struct xfs_mount	*mp,
770	int			error_class,
771	int			error)
772{
773	struct xfs_error_cfg	*cfg;
774
775	if (error < 0)
776		error = -error;
777
778	switch (error) {
779	case EIO:
780		cfg = &mp->m_error_cfg[error_class][XFS_ERR_EIO];
781		break;
782	case ENOSPC:
783		cfg = &mp->m_error_cfg[error_class][XFS_ERR_ENOSPC];
784		break;
785	case ENODEV:
786		cfg = &mp->m_error_cfg[error_class][XFS_ERR_ENODEV];
787		break;
788	default:
789		cfg = &mp->m_error_cfg[error_class][XFS_ERR_DEFAULT];
790		break;
791	}
792
793	return cfg;
794}
v4.10.11
 
  1/*
  2 * Copyright (c) 2014 Red Hat, Inc.
  3 * All Rights Reserved.
  4 *
  5 * This program is free software; you can redistribute it and/or
  6 * modify it under the terms of the GNU General Public License as
  7 * published by the Free Software Foundation.
  8 *
  9 * This program is distributed in the hope that it would be useful,
 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 * GNU General Public License for more details.
 13 *
 14 * You should have received a copy of the GNU General Public License
 15 * along with this program; if not, write the Free Software Foundation,
 16 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 17 */
 18
 19#include "xfs.h"
 20#include "xfs_shared.h"
 21#include "xfs_format.h"
 22#include "xfs_log_format.h"
 23#include "xfs_trans_resv.h"
 24#include "xfs_sysfs.h"
 25#include "xfs_log.h"
 26#include "xfs_log_priv.h"
 27#include "xfs_stats.h"
 28#include "xfs_mount.h"
 29
 30struct xfs_sysfs_attr {
 31	struct attribute attr;
 32	ssize_t (*show)(struct kobject *kobject, char *buf);
 33	ssize_t (*store)(struct kobject *kobject, const char *buf,
 34			 size_t count);
 35};
 36
 37static inline struct xfs_sysfs_attr *
 38to_attr(struct attribute *attr)
 39{
 40	return container_of(attr, struct xfs_sysfs_attr, attr);
 41}
 42
 43#define XFS_SYSFS_ATTR_RW(name) \
 44	static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_RW(name)
 45#define XFS_SYSFS_ATTR_RO(name) \
 46	static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_RO(name)
 47#define XFS_SYSFS_ATTR_WO(name) \
 48	static struct xfs_sysfs_attr xfs_sysfs_attr_##name = __ATTR_WO(name)
 49
 50#define ATTR_LIST(name) &xfs_sysfs_attr_##name.attr
 51
 52STATIC ssize_t
 53xfs_sysfs_object_show(
 54	struct kobject		*kobject,
 55	struct attribute	*attr,
 56	char			*buf)
 57{
 58	struct xfs_sysfs_attr *xfs_attr = to_attr(attr);
 59
 60	return xfs_attr->show ? xfs_attr->show(kobject, buf) : 0;
 61}
 62
 63STATIC ssize_t
 64xfs_sysfs_object_store(
 65	struct kobject		*kobject,
 66	struct attribute	*attr,
 67	const char		*buf,
 68	size_t			count)
 69{
 70	struct xfs_sysfs_attr *xfs_attr = to_attr(attr);
 71
 72	return xfs_attr->store ? xfs_attr->store(kobject, buf, count) : 0;
 73}
 74
 75static const struct sysfs_ops xfs_sysfs_ops = {
 76	.show = xfs_sysfs_object_show,
 77	.store = xfs_sysfs_object_store,
 78};
 79
 80/*
 81 * xfs_mount kobject. The mp kobject also serves as the per-mount parent object
 82 * that is identified by the fsname under sysfs.
 83 */
 84
 85static inline struct xfs_mount *
 86to_mp(struct kobject *kobject)
 87{
 88	struct xfs_kobj *kobj = to_kobj(kobject);
 89
 90	return container_of(kobj, struct xfs_mount, m_kobj);
 91}
 92
 93#ifdef DEBUG
 
 94
 95STATIC ssize_t
 96fail_writes_store(
 97	struct kobject		*kobject,
 98	const char		*buf,
 99	size_t			count)
100{
101	struct xfs_mount	*mp = to_mp(kobject);
102	int			ret;
103	int			val;
104
105	ret = kstrtoint(buf, 0, &val);
106	if (ret)
107		return ret;
108
109	if (val == 1)
110		mp->m_fail_writes = true;
111	else if (val == 0)
112		mp->m_fail_writes = false;
113	else
114		return -EINVAL;
115
116	return count;
117}
118
119STATIC ssize_t
120fail_writes_show(
121	struct kobject		*kobject,
122	char			*buf)
123{
124	struct xfs_mount	*mp = to_mp(kobject);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
126	return snprintf(buf, PAGE_SIZE, "%d\n", mp->m_fail_writes ? 1 : 0);
 
 
 
 
 
 
 
 
127}
128XFS_SYSFS_ATTR_RW(fail_writes);
129
130#endif /* DEBUG */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
132static struct attribute *xfs_mp_attrs[] = {
133#ifdef DEBUG
134	ATTR_LIST(fail_writes),
135#endif
136	NULL,
137};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
139struct kobj_type xfs_mp_ktype = {
140	.release = xfs_sysfs_release,
141	.sysfs_ops = &xfs_sysfs_ops,
142	.default_attrs = xfs_mp_attrs,
143};
144
145#ifdef DEBUG
146/* debug */
 
 
 
 
 
 
 
147
148STATIC ssize_t
149log_recovery_delay_store(
150	struct kobject	*kobject,
151	const char	*buf,
152	size_t		count)
153{
154	int		ret;
155	int		val;
156
157	ret = kstrtoint(buf, 0, &val);
158	if (ret)
159		return ret;
160
161	if (val < 0 || val > 60)
162		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
164	xfs_globals.log_recovery_delay = val;
 
 
165
 
166	return count;
167}
168
169STATIC ssize_t
170log_recovery_delay_show(
171	struct kobject	*kobject,
172	char		*buf)
173{
174	return snprintf(buf, PAGE_SIZE, "%d\n", xfs_globals.log_recovery_delay);
175}
176XFS_SYSFS_ATTR_RW(log_recovery_delay);
177
178static struct attribute *xfs_dbg_attrs[] = {
 
179	ATTR_LIST(log_recovery_delay),
 
 
 
 
 
 
 
 
180	NULL,
181};
 
182
183struct kobj_type xfs_dbg_ktype = {
184	.release = xfs_sysfs_release,
185	.sysfs_ops = &xfs_sysfs_ops,
186	.default_attrs = xfs_dbg_attrs,
187};
188
189#endif /* DEBUG */
190
191/* stats */
192
193static inline struct xstats *
194to_xstats(struct kobject *kobject)
195{
196	struct xfs_kobj *kobj = to_kobj(kobject);
197
198	return container_of(kobj, struct xstats, xs_kobj);
199}
200
201STATIC ssize_t
202stats_show(
203	struct kobject	*kobject,
204	char		*buf)
205{
206	struct xstats	*stats = to_xstats(kobject);
207
208	return xfs_stats_format(stats->xs_stats, buf);
209}
210XFS_SYSFS_ATTR_RO(stats);
211
212STATIC ssize_t
213stats_clear_store(
214	struct kobject	*kobject,
215	const char	*buf,
216	size_t		count)
217{
218	int		ret;
219	int		val;
220	struct xstats	*stats = to_xstats(kobject);
221
222	ret = kstrtoint(buf, 0, &val);
223	if (ret)
224		return ret;
225
226	if (val != 1)
227		return -EINVAL;
228
229	xfs_stats_clearall(stats->xs_stats);
230	return count;
231}
232XFS_SYSFS_ATTR_WO(stats_clear);
233
234static struct attribute *xfs_stats_attrs[] = {
235	ATTR_LIST(stats),
236	ATTR_LIST(stats_clear),
237	NULL,
238};
 
239
240struct kobj_type xfs_stats_ktype = {
241	.release = xfs_sysfs_release,
242	.sysfs_ops = &xfs_sysfs_ops,
243	.default_attrs = xfs_stats_attrs,
244};
245
246/* xlog */
247
248static inline struct xlog *
249to_xlog(struct kobject *kobject)
250{
251	struct xfs_kobj *kobj = to_kobj(kobject);
252
253	return container_of(kobj, struct xlog, l_kobj);
254}
255
256STATIC ssize_t
257log_head_lsn_show(
258	struct kobject	*kobject,
259	char		*buf)
260{
261	int cycle;
262	int block;
263	struct xlog *log = to_xlog(kobject);
264
265	spin_lock(&log->l_icloglock);
266	cycle = log->l_curr_cycle;
267	block = log->l_curr_block;
268	spin_unlock(&log->l_icloglock);
269
270	return snprintf(buf, PAGE_SIZE, "%d:%d\n", cycle, block);
271}
272XFS_SYSFS_ATTR_RO(log_head_lsn);
273
274STATIC ssize_t
275log_tail_lsn_show(
276	struct kobject	*kobject,
277	char		*buf)
278{
279	int cycle;
280	int block;
281	struct xlog *log = to_xlog(kobject);
282
283	xlog_crack_atomic_lsn(&log->l_tail_lsn, &cycle, &block);
284	return snprintf(buf, PAGE_SIZE, "%d:%d\n", cycle, block);
285}
286XFS_SYSFS_ATTR_RO(log_tail_lsn);
287
288STATIC ssize_t
289reserve_grant_head_show(
290	struct kobject	*kobject,
291	char		*buf)
292
293{
294	int cycle;
295	int bytes;
296	struct xlog *log = to_xlog(kobject);
297
298	xlog_crack_grant_head(&log->l_reserve_head.grant, &cycle, &bytes);
299	return snprintf(buf, PAGE_SIZE, "%d:%d\n", cycle, bytes);
300}
301XFS_SYSFS_ATTR_RO(reserve_grant_head);
302
303STATIC ssize_t
304write_grant_head_show(
305	struct kobject	*kobject,
306	char		*buf)
307{
308	int cycle;
309	int bytes;
310	struct xlog *log = to_xlog(kobject);
311
312	xlog_crack_grant_head(&log->l_write_head.grant, &cycle, &bytes);
313	return snprintf(buf, PAGE_SIZE, "%d:%d\n", cycle, bytes);
314}
315XFS_SYSFS_ATTR_RO(write_grant_head);
316
317#ifdef DEBUG
318STATIC ssize_t
319log_badcrc_factor_store(
320	struct kobject	*kobject,
321	const char	*buf,
322	size_t		count)
323{
324	struct xlog	*log = to_xlog(kobject);
325	int		ret;
326	uint32_t	val;
327
328	ret = kstrtouint(buf, 0, &val);
329	if (ret)
330		return ret;
331
332	log->l_badcrc_factor = val;
333
334	return count;
335}
336
337STATIC ssize_t
338log_badcrc_factor_show(
339	struct kobject	*kobject,
340	char		*buf)
341{
342	struct xlog	*log = to_xlog(kobject);
343
344	return snprintf(buf, PAGE_SIZE, "%d\n", log->l_badcrc_factor);
345}
346
347XFS_SYSFS_ATTR_RW(log_badcrc_factor);
348#endif	/* DEBUG */
349
350static struct attribute *xfs_log_attrs[] = {
351	ATTR_LIST(log_head_lsn),
352	ATTR_LIST(log_tail_lsn),
353	ATTR_LIST(reserve_grant_head),
354	ATTR_LIST(write_grant_head),
355#ifdef DEBUG
356	ATTR_LIST(log_badcrc_factor),
357#endif
358	NULL,
359};
 
360
361struct kobj_type xfs_log_ktype = {
362	.release = xfs_sysfs_release,
363	.sysfs_ops = &xfs_sysfs_ops,
364	.default_attrs = xfs_log_attrs,
365};
366
367/*
368 * Metadata IO error configuration
369 *
370 * The sysfs structure here is:
371 *	...xfs/<dev>/error/<class>/<errno>/<error_attrs>
372 *
373 * where <class> allows us to discriminate between data IO and metadata IO,
374 * and any other future type of IO (e.g. special inode or directory error
375 * handling) we care to support.
376 */
377static inline struct xfs_error_cfg *
378to_error_cfg(struct kobject *kobject)
379{
380	struct xfs_kobj *kobj = to_kobj(kobject);
381	return container_of(kobj, struct xfs_error_cfg, kobj);
382}
383
384static inline struct xfs_mount *
385err_to_mp(struct kobject *kobject)
386{
387	struct xfs_kobj *kobj = to_kobj(kobject);
388	return container_of(kobj, struct xfs_mount, m_error_kobj);
389}
390
391static ssize_t
392max_retries_show(
393	struct kobject	*kobject,
394	char		*buf)
395{
396	int		retries;
397	struct xfs_error_cfg *cfg = to_error_cfg(kobject);
398
399	if (cfg->max_retries == XFS_ERR_RETRY_FOREVER)
400		retries = -1;
401	else
402		retries = cfg->max_retries;
403
404	return snprintf(buf, PAGE_SIZE, "%d\n", retries);
405}
406
407static ssize_t
408max_retries_store(
409	struct kobject	*kobject,
410	const char	*buf,
411	size_t		count)
412{
413	struct xfs_error_cfg *cfg = to_error_cfg(kobject);
414	int		ret;
415	int		val;
416
417	ret = kstrtoint(buf, 0, &val);
418	if (ret)
419		return ret;
420
421	if (val < -1)
422		return -EINVAL;
423
424	if (val == -1)
425		cfg->max_retries = XFS_ERR_RETRY_FOREVER;
426	else
427		cfg->max_retries = val;
428	return count;
429}
430XFS_SYSFS_ATTR_RW(max_retries);
431
432static ssize_t
433retry_timeout_seconds_show(
434	struct kobject	*kobject,
435	char		*buf)
436{
437	int		timeout;
438	struct xfs_error_cfg *cfg = to_error_cfg(kobject);
439
440	if (cfg->retry_timeout == XFS_ERR_RETRY_FOREVER)
441		timeout = -1;
442	else
443		timeout = jiffies_to_msecs(cfg->retry_timeout) / MSEC_PER_SEC;
444
445	return snprintf(buf, PAGE_SIZE, "%d\n", timeout);
446}
447
448static ssize_t
449retry_timeout_seconds_store(
450	struct kobject	*kobject,
451	const char	*buf,
452	size_t		count)
453{
454	struct xfs_error_cfg *cfg = to_error_cfg(kobject);
455	int		ret;
456	int		val;
457
458	ret = kstrtoint(buf, 0, &val);
459	if (ret)
460		return ret;
461
462	/* 1 day timeout maximum, -1 means infinite */
463	if (val < -1 || val > 86400)
464		return -EINVAL;
465
466	if (val == -1)
467		cfg->retry_timeout = XFS_ERR_RETRY_FOREVER;
468	else {
469		cfg->retry_timeout = msecs_to_jiffies(val * MSEC_PER_SEC);
470		ASSERT(msecs_to_jiffies(val * MSEC_PER_SEC) < LONG_MAX);
471	}
472	return count;
473}
474XFS_SYSFS_ATTR_RW(retry_timeout_seconds);
475
476static ssize_t
477fail_at_unmount_show(
478	struct kobject	*kobject,
479	char		*buf)
480{
481	struct xfs_mount	*mp = err_to_mp(kobject);
482
483	return snprintf(buf, PAGE_SIZE, "%d\n", mp->m_fail_unmount);
484}
485
486static ssize_t
487fail_at_unmount_store(
488	struct kobject	*kobject,
489	const char	*buf,
490	size_t		count)
491{
492	struct xfs_mount	*mp = err_to_mp(kobject);
493	int		ret;
494	int		val;
495
496	ret = kstrtoint(buf, 0, &val);
497	if (ret)
498		return ret;
499
500	if (val < 0 || val > 1)
501		return -EINVAL;
502
503	mp->m_fail_unmount = val;
504	return count;
505}
506XFS_SYSFS_ATTR_RW(fail_at_unmount);
507
508static struct attribute *xfs_error_attrs[] = {
509	ATTR_LIST(max_retries),
510	ATTR_LIST(retry_timeout_seconds),
511	NULL,
512};
 
513
514
515static struct kobj_type xfs_error_cfg_ktype = {
516	.release = xfs_sysfs_release,
517	.sysfs_ops = &xfs_sysfs_ops,
518	.default_attrs = xfs_error_attrs,
519};
520
521static struct kobj_type xfs_error_ktype = {
522	.release = xfs_sysfs_release,
523	.sysfs_ops = &xfs_sysfs_ops,
524};
525
526/*
527 * Error initialization tables. These need to be ordered in the same
528 * order as the enums used to index the array. All class init tables need to
529 * define a "default" behaviour as the first entry, all other entries can be
530 * empty.
531 */
532struct xfs_error_init {
533	char		*name;
534	int		max_retries;
535	int		retry_timeout;	/* in seconds */
536};
537
538static const struct xfs_error_init xfs_error_meta_init[XFS_ERR_ERRNO_MAX] = {
539	{ .name = "default",
540	  .max_retries = XFS_ERR_RETRY_FOREVER,
541	  .retry_timeout = XFS_ERR_RETRY_FOREVER,
542	},
543	{ .name = "EIO",
544	  .max_retries = XFS_ERR_RETRY_FOREVER,
545	  .retry_timeout = XFS_ERR_RETRY_FOREVER,
546	},
547	{ .name = "ENOSPC",
548	  .max_retries = XFS_ERR_RETRY_FOREVER,
549	  .retry_timeout = XFS_ERR_RETRY_FOREVER,
550	},
551	{ .name = "ENODEV",
552	  .max_retries = 0,	/* We can't recover from devices disappearing */
553	  .retry_timeout = 0,
554	},
555};
556
557static int
558xfs_error_sysfs_init_class(
559	struct xfs_mount	*mp,
560	int			class,
561	const char		*parent_name,
562	struct xfs_kobj		*parent_kobj,
563	const struct xfs_error_init init[])
564{
565	struct xfs_error_cfg	*cfg;
566	int			error;
567	int			i;
568
569	ASSERT(class < XFS_ERR_CLASS_MAX);
570
571	error = xfs_sysfs_init(parent_kobj, &xfs_error_ktype,
572				&mp->m_error_kobj, parent_name);
573	if (error)
574		return error;
575
576	for (i = 0; i < XFS_ERR_ERRNO_MAX; i++) {
577		cfg = &mp->m_error_cfg[class][i];
578		error = xfs_sysfs_init(&cfg->kobj, &xfs_error_cfg_ktype,
579					parent_kobj, init[i].name);
580		if (error)
581			goto out_error;
582
583		cfg->max_retries = init[i].max_retries;
584		if (init[i].retry_timeout == XFS_ERR_RETRY_FOREVER)
585			cfg->retry_timeout = XFS_ERR_RETRY_FOREVER;
586		else
587			cfg->retry_timeout = msecs_to_jiffies(
588					init[i].retry_timeout * MSEC_PER_SEC);
589	}
590	return 0;
591
592out_error:
593	/* unwind the entries that succeeded */
594	for (i--; i >= 0; i--) {
595		cfg = &mp->m_error_cfg[class][i];
596		xfs_sysfs_del(&cfg->kobj);
597	}
598	xfs_sysfs_del(parent_kobj);
599	return error;
600}
601
602int
603xfs_error_sysfs_init(
604	struct xfs_mount	*mp)
605{
606	int			error;
607
608	/* .../xfs/<dev>/error/ */
609	error = xfs_sysfs_init(&mp->m_error_kobj, &xfs_error_ktype,
610				&mp->m_kobj, "error");
611	if (error)
612		return error;
613
614	error = sysfs_create_file(&mp->m_error_kobj.kobject,
615				  ATTR_LIST(fail_at_unmount));
616
617	if (error)
618		goto out_error;
619
620	/* .../xfs/<dev>/error/metadata/ */
621	error = xfs_error_sysfs_init_class(mp, XFS_ERR_METADATA,
622				"metadata", &mp->m_error_meta_kobj,
623				xfs_error_meta_init);
624	if (error)
625		goto out_error;
626
627	return 0;
628
629out_error:
630	xfs_sysfs_del(&mp->m_error_kobj);
631	return error;
632}
633
634void
635xfs_error_sysfs_del(
636	struct xfs_mount	*mp)
637{
638	struct xfs_error_cfg	*cfg;
639	int			i, j;
640
641	for (i = 0; i < XFS_ERR_CLASS_MAX; i++) {
642		for (j = 0; j < XFS_ERR_ERRNO_MAX; j++) {
643			cfg = &mp->m_error_cfg[i][j];
644
645			xfs_sysfs_del(&cfg->kobj);
646		}
647	}
648	xfs_sysfs_del(&mp->m_error_meta_kobj);
649	xfs_sysfs_del(&mp->m_error_kobj);
650}
651
652struct xfs_error_cfg *
653xfs_error_get_cfg(
654	struct xfs_mount	*mp,
655	int			error_class,
656	int			error)
657{
658	struct xfs_error_cfg	*cfg;
659
660	if (error < 0)
661		error = -error;
662
663	switch (error) {
664	case EIO:
665		cfg = &mp->m_error_cfg[error_class][XFS_ERR_EIO];
666		break;
667	case ENOSPC:
668		cfg = &mp->m_error_cfg[error_class][XFS_ERR_ENOSPC];
669		break;
670	case ENODEV:
671		cfg = &mp->m_error_cfg[error_class][XFS_ERR_ENODEV];
672		break;
673	default:
674		cfg = &mp->m_error_cfg[error_class][XFS_ERR_DEFAULT];
675		break;
676	}
677
678	return cfg;
679}