summaryrefslogtreecommitdiffstats
path: root/fs/xfs/scrub/xfblob.c
blob: 6ef2a9637f162d7589472bb910a2808849f14e4c (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2021-2024 Oracle.  All Rights Reserved.
 * Author: Darrick J. Wong <djwong@kernel.org>
 */
#include "xfs.h"
#include "xfs_fs.h"
#include "xfs_shared.h"
#include "xfs_format.h"
#include "scrub/scrub.h"
#include "scrub/xfile.h"
#include "scrub/xfarray.h"
#include "scrub/xfblob.h"

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

#define XB_KEY_MAGIC	0xABAADDAD
struct xb_key {
	uint32_t		xb_magic;  /* XB_KEY_MAGIC */
	uint32_t		xb_size;   /* size of the blob, in bytes */
	loff_t			xb_offset; /* byte offset of this key */
	/* blob comes after here */
} __packed;

/* Initialize a blob storage object. */
int
xfblob_create(
	const char		*description,
	struct xfblob		**blobp)
{
	struct xfblob		*blob;
	struct xfile		*xfile;
	int			error;

	error = xfile_create(description, 0, &xfile);
	if (error)
		return error;

	blob = kmalloc(sizeof(struct xfblob), XCHK_GFP_FLAGS);
	if (!blob) {
		error = -ENOMEM;
		goto out_xfile;
	}

	blob->xfile = xfile;
	blob->last_offset = PAGE_SIZE;

	*blobp = blob;
	return 0;

out_xfile:
	xfile_destroy(xfile);
	return error;
}

/* Destroy a blob storage object. */
void
xfblob_destroy(
	struct xfblob	*blob)
{
	xfile_destroy(blob->xfile);
	kfree(blob);
}

/* Retrieve a blob. */
int
xfblob_load(
	struct xfblob	*blob,
	xfblob_cookie	cookie,
	void		*ptr,
	uint32_t	size)
{
	struct xb_key	key;
	int		error;

	error = xfile_load(blob->xfile, &key, sizeof(key), cookie);
	if (error)
		return error;

	if (key.xb_magic != XB_KEY_MAGIC || key.xb_offset != cookie) {
		ASSERT(0);
		return -ENODATA;
	}
	if (size < key.xb_size) {
		ASSERT(0);
		return -EFBIG;
	}

	return xfile_load(blob->xfile, ptr, key.xb_size,
			cookie + sizeof(key));
}

/* Store a blob. */
int
xfblob_store(
	struct xfblob	*blob,
	xfblob_cookie	*cookie,
	const void	*ptr,
	uint32_t	size)
{
	struct xb_key	key = {
		.xb_offset = blob->last_offset,
		.xb_magic = XB_KEY_MAGIC,
		.xb_size = size,
	};
	loff_t		pos = blob->last_offset;
	int		error;

	error = xfile_store(blob->xfile, &key, sizeof(key), pos);
	if (error)
		return error;

	pos += sizeof(key);
	error = xfile_store(blob->xfile, ptr, size, pos);
	if (error)
		goto out_err;

	*cookie = blob->last_offset;
	blob->last_offset += sizeof(key) + size;
	return 0;
out_err:
	xfile_discard(blob->xfile, blob->last_offset, sizeof(key));
	return error;
}

/* Free a blob. */
int
xfblob_free(
	struct xfblob	*blob,
	xfblob_cookie	cookie)
{
	struct xb_key	key;
	int		error;

	error = xfile_load(blob->xfile, &key, sizeof(key), cookie);
	if (error)
		return error;

	if (key.xb_magic != XB_KEY_MAGIC || key.xb_offset != cookie) {
		ASSERT(0);
		return -ENODATA;
	}

	xfile_discard(blob->xfile, cookie, sizeof(key) + key.xb_size);
	return 0;
}

/* How many bytes is this blob storage object consuming? */
unsigned long long
xfblob_bytes(
	struct xfblob		*blob)
{
	return xfile_bytes(blob->xfile);
}

/* Drop all the blobs. */
void
xfblob_truncate(
	struct xfblob	*blob)
{
	xfile_discard(blob->xfile, PAGE_SIZE, MAX_LFS_FILESIZE - PAGE_SIZE);
	blob->last_offset = PAGE_SIZE;
}