summaryrefslogtreecommitdiff
path: root/lib/sqfs/write_xattr.c
blob: 2263fbecda5d60d71300f658102c749954b4ca40 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * write_xattr.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

#include "meta_writer.h"
#include "highlevel.h"
#include "util.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

static int write_key(meta_writer_t *mw, const char *key, tree_xattr_t *xattr,
		     bool value_is_ool)
{
	sqfs_xattr_entry_t kent;
	int type;

	type = sqfs_get_xattr_prefix_id(key);
	if (type < 0) {
		fprintf(stderr, "unsupported xattr key '%s'\n", key);
		return -1;
	}

	key = strchr(key, '.');
	assert(key != NULL);
	++key;

	if (value_is_ool)
		type |= SQUASHFS_XATTR_FLAG_OOL;

	kent.type = htole16(type);
	kent.size = htole16(strlen(key));

	if (meta_writer_append(mw, &kent, sizeof(kent)))
		return -1;
	if (meta_writer_append(mw, key, strlen(key)))
		return -1;

	xattr->size += sizeof(sqfs_xattr_entry_t) + strlen(key);
	return 0;
}

static int write_value(meta_writer_t *mw, const char *value,
		       tree_xattr_t *xattr, uint64_t *value_ref_out)
{
	sqfs_xattr_value_t vent;
	uint32_t offset;
	uint64_t block;

	meta_writer_get_position(mw, &block, &offset);
	*value_ref_out = (block << 16) | (offset & 0xFFFF);

	vent.size = htole32(strlen(value));

	if (meta_writer_append(mw, &vent, sizeof(vent)))
		return -1;

	if (meta_writer_append(mw, value, strlen(value)))
		return -1;

	xattr->size += sizeof(vent) + strlen(value);
	return 0;
}

static int write_value_ool(meta_writer_t *mw, uint64_t location,
			   tree_xattr_t *xattr)
{
	sqfs_xattr_value_t vent;
	uint64_t ref;

	vent.size = htole32(sizeof(location));
	if (meta_writer_append(mw, &vent, sizeof(vent)))
		return -1;

	ref = htole64(location);
	if (meta_writer_append(mw, &ref, sizeof(ref)))
		return -1;

	xattr->size += sizeof(vent) + sizeof(ref);
	return 0;
}

static bool should_store_ool(fstree_t *fs, const char *value, size_t index)
{
	size_t refcount;

	refcount = str_table_get_ref_count(&fs->xattr_values, index);
	if (refcount < 2)
		return false;

	/*
	  Storing in line needs this many bytes: refcount * len

	  Storing out-of-line needs this many: len + (refcount - 1) * 8

	  Out-of-line prefereable if:
	      refcount * len > len + (refcount - 1) * 8
	   => refcount * len - len > (refcount - 1) * 8
	   => (refcount - 1) * len > (refcount - 1) * 8
	   => len > 8

	   Note that this only holds iff refcount - 1 != 0, i.e. refcount > 1,
	   otherwise we would be dividing by 0 in the 3rd step.
	 */
	return strlen(value) > sizeof(uint64_t);
}

static int write_kv_pairs(fstree_t *fs, meta_writer_t *mw, tree_xattr_t *xattr,
			  uint64_t *ool_locations)
{
	uint32_t key_idx, val_idx;
	const char *key, *value;
	uint64_t ref;
	size_t i;

	for (i = 0; i < xattr->num_attr; ++i) {
		key_idx = xattr->attr[i].key_index;
		val_idx = xattr->attr[i].value_index;

		key = str_table_get_string(&fs->xattr_keys, key_idx);
		value = str_table_get_string(&fs->xattr_values, val_idx);

		if (ool_locations[val_idx] == 0xFFFFFFFFFFFFFFFF) {
			if (write_key(mw, key, xattr, false))
				return -1;

			if (write_value(mw, value, xattr, &ref))
				return -1;

			if (should_store_ool(fs, value, val_idx))
				ool_locations[val_idx] = ref;
		} else {
			if (write_key(mw, key, xattr, true))
				return -1;

			if (write_value_ool(mw, ool_locations[val_idx], xattr))
				return -1;
		}
	}

	return 0;
}

static uint64_t *create_ool_locations_table(fstree_t *fs)
{
	uint64_t *table;
	size_t i;

	table = alloc_array(sizeof(uint64_t), fs->xattr_values.num_strings);

	if (table == NULL) {
		perror("allocating Xattr OOL locations table");
		return NULL;
	}

	for (i = 0; i < fs->xattr_values.num_strings; ++i) {
		table[i] = 0xFFFFFFFFFFFFFFFFUL;
	}

	return table;
}

int write_xattr(int outfd, fstree_t *fs, sqfs_super_t *super,
		compressor_t *cmp)
{
	uint64_t kv_start, id_start, block, *tbl, *ool_locations;
	size_t i = 0, count = 0, blocks;
	sqfs_xattr_id_table_t idtbl;
	sqfs_xattr_id_t id_ent;
	meta_writer_t *mw;
	tree_xattr_t *it;
	uint32_t offset;

	if (fs->xattr == NULL)
		return 0;

	ool_locations = create_ool_locations_table(fs);
	if (ool_locations == NULL)
		return -1;

	mw = meta_writer_create(outfd, cmp, false);
	if (mw == NULL)
		goto fail_ool;

	/* write xattr key-value pairs */
	kv_start = super->bytes_used;

	for (it = fs->xattr; it != NULL; it = it->next) {
		meta_writer_get_position(mw, &it->block, &it->offset);
		it->size = 0;

		if (write_kv_pairs(fs, mw, it, ool_locations))
			goto fail_mw;

		++count;
	}

	if (meta_writer_flush(mw))
		goto fail_mw;

	meta_writer_get_position(mw, &block, &offset);
	meta_writer_reset(mw);

	super->bytes_used += block;

	/* allocate location table */
	blocks = (count * sizeof(id_ent)) / SQFS_META_BLOCK_SIZE;

	if ((count * sizeof(id_ent)) % SQFS_META_BLOCK_SIZE)
		++blocks;

	tbl = alloc_array(sizeof(uint64_t), blocks);

	if (tbl == NULL) {
		perror("generating xattr ID table");
		goto fail_mw;
	}

	/* write ID table referring to key value pairs, record offsets */
	id_start = 0;
	tbl[i++] = htole64(super->bytes_used);

	for (it = fs->xattr; it != NULL; it = it->next) {
		id_ent.xattr = htole64((it->block << 16) | it->offset);
		id_ent.count = htole32(it->num_attr);
		id_ent.size = htole32(it->size);

		if (meta_writer_append(mw, &id_ent, sizeof(id_ent)))
			goto fail_tbl;

		meta_writer_get_position(mw, &block, &offset);

		if (block != id_start) {
			id_start = block;
			tbl[i++] = htole64(super->bytes_used + id_start);
		}
	}

	if (meta_writer_flush(mw))
		goto fail_tbl;

	meta_writer_get_position(mw, &block, &offset);
	super->bytes_used += block;

	/* write offset table */
	idtbl.xattr_table_start = htole64(kv_start);
	idtbl.xattr_ids = htole32(count);
	idtbl.unused = 0;

	if (write_data("writing xattr ID table", outfd, &idtbl, sizeof(idtbl)))
		goto fail_tbl;

	if (write_data("writing xattr ID table",
		       outfd, tbl, sizeof(tbl[0]) * blocks)) {
		goto fail_tbl;
	}

	super->xattr_id_table_start = super->bytes_used;
	super->bytes_used += sizeof(idtbl) + sizeof(tbl[0]) * blocks;
	super->flags &= ~SQFS_FLAG_NO_XATTRS;

	free(tbl);
	meta_writer_destroy(mw);
	free(ool_locations);
	return 0;
fail_tbl:
	free(tbl);
fail_mw:
	meta_writer_destroy(mw);
fail_ool:
	free(ool_locations);
	return -1;
}