summaryrefslogtreecommitdiff
path: root/fs/xfs/scrub/blob.c
blob: 4928f0985d49568b399df9e930178622b14e994e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2019 Oracle.  All Rights Reserved.
 * Author: Darrick J. Wong <darrick.wong@oracle.com>
 */
#include "xfs.h"
#include "xfs_fs.h"
#include "xfs_shared.h"
#include "scrub/array.h"
#include "scrub/blob.h"

/*
 * XFS Blob Storage
 * ================
 * Stores and retrieves blobs using a list.  Objects are appended to
 * the list and the pointer is returned as a magic cookie for retrieval.
 */

#define XB_KEY_MAGIC	0xABAADDAD
struct xb_key {
	struct list_head	list;
	uint32_t		magic;
	uint32_t		size;
	/* blob comes after here */
} __packed;

#define XB_KEY_SIZE(sz)	(sizeof(struct xb_key) + (sz))

/* Initialize a blob storage object. */
struct xblob *
xblob_init(void)
{
	struct xblob	*blob;
	int		error;

	error = -ENOMEM;
	blob = kmem_alloc(sizeof(struct xblob), KM_NOFS | KM_MAYFAIL);
	if (!blob)
		return ERR_PTR(error);

	INIT_LIST_HEAD(&blob->list);
	return blob;
}

/* Destroy a blob storage object. */
void
xblob_destroy(
	struct xblob	*blob)
{
	struct xb_key	*key, *n;

	list_for_each_entry_safe(key, n, &blob->list, list) {
		list_del(&key->list);
		kmem_free(key);
	}
	kmem_free(blob);
}

/* Retrieve a blob. */
int
xblob_get(
	struct xblob	*blob,
	xblob_cookie	cookie,
	void		*ptr,
	uint32_t	size)
{
	struct xb_key	*key = (struct xb_key *)cookie;

	if (key->magic != XB_KEY_MAGIC) {
		ASSERT(0);
		return -ENODATA;
	}
	if (size < key->size) {
		ASSERT(0);
		return -EFBIG;
	}

	memcpy(ptr, key + 1, key->size);
	return 0;
}

/* Store a blob. */
int
xblob_put(
	struct xblob	*blob,
	xblob_cookie	*cookie,
	void		*ptr,
	uint32_t	size)
{
	struct xb_key	*key;

	key = kmem_alloc(XB_KEY_SIZE(size), KM_NOFS | KM_MAYFAIL);
	if (!key)
		return -ENOMEM;

	INIT_LIST_HEAD(&key->list);
	list_add_tail(&key->list, &blob->list);
	key->magic = XB_KEY_MAGIC;
	key->size = size;
	memcpy(key + 1, ptr, size);
	*cookie = (xblob_cookie)key;
	return 0;
}

/* Free a blob. */
int
xblob_free(
	struct xblob	*blob,
	xblob_cookie	cookie)
{
	struct xb_key	*key = (struct xb_key *)cookie;

	if (key->magic != XB_KEY_MAGIC) {
		ASSERT(0);
		return -ENODATA;
	}
	key->magic = 0;
	list_del(&key->list);
	kmem_free(key);
	return 0;
}